-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient01.c
77 lines (66 loc) · 1.71 KB
/
client01.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
75
76
77
/*************************************************************************
> File Name: client01.c
> Author: lidongmeng
> Mail:lidongmeng0213@163.com
> Created Time: Mon 31 Aug 2015 11:22:44 PM PDT
************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1024
int main(int argc, char * argv[]) {
char buf[MAXLINE];
int sockfd;
char * serverIP = "127.0.0.1";
int serverPort = 6888;
struct sockaddr_in serverAddr;
int status;
int n;
char sendLine[MAXLINE], recvLine[MAXLINE];
if (argc == 2) {
serverIP = argv[1];
}
if (argc == 3) {
serverIP = argv[1];
serverPort = atoi(argv[2]);
}
if (argc > 3) {
printf("usage: client01 <IPAddr> <Port>\n");
return -1;
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Error in socket build\n");
return sockfd;
}
bzero(&serverAddr, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(serverPort);
inet_pton(AF_INET, serverIP, &serverAddr.sin_addr);
if (status = connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
printf("connect error\n");
return status;
}
printf("client connnect to the server\n");
while (1) {
if (fgets(sendLine, MAXLINE, stdin) == NULL) {
break;
}
n = write(sockfd, sendLine, strlen(sendLine));
n = read(sockfd, recvLine, MAXLINE);
if (n == 0) {
printf("server terminate\n");
break;
}
fputs(recvLine, stdout);
// write(STDOUT_FILENO, recvLine, n);
}
close(sockfd);
printf("End\n");
return 0;
}