forked from rqycpp/socket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
74 lines (64 loc) · 1.41 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//客户端程序:
//向服务端发送字符串,经过大小写变换后返回。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#define PORT 1234
#define MAXDATASIZE 256
int main(int argc, char *argv[])
{
int sockfd, num;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in server;
if(argc != 2)
{
printf("Usage: %s <IP Address>\n", argv[0]);
exit(1);
}
if((he = gethostbyname(argv[1])) == NULL)
{
printf("gethostbyname() error.\n");
exit(1);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("socket() error.\n");
exit(1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr);
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("connect() error.\n");
exit(1);
}
//if((num = recv(sockfd, buf, MAXDATASIZE)) == -1)
//{
//printf("recv() error.\n");
//exit(1);
//}
printf("Connected to server.\n");
while(1){
printf("Input:");
//gets(buf);//dangerous
fgets(buf, MAXDATASIZE, stdin);
write(sockfd, buf, strlen(buf));
if((num = read(sockfd,buf, MAXDATASIZE)) == -1){
printf("read() error.\n");
exit(1);
}
buf[num] = '\0';
printf("Output:%s", buf);
}
close(sockfd);
return 0;
}