当前位置: 技术问答>linux和unix
一个简单socket程序问题
来源: 互联网 发布时间:2016-12-21
本文导语: 就是客户端发送字符串服务端进行大小写转换,把结果显示出来,但显示结果有问题,没有转换成功,原样传回来了,显示客户端口也出现问题,端口号是8000,显示出来却是59363,请大家指教一下!谢谢! waiting ......
就是客户端发送字符串服务端进行大小写转换,把结果显示出来,但显示结果有问题,没有转换成功,原样传回来了,显示客户端口也出现问题,端口号是8000,显示出来却是59363,请大家指教一下!谢谢!
waiting ...
client IP is 127.0.0.1, port is 59363
client is : Test String
receive from server: Test String
server.c
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8
9 #define MAX_LINE 100
10
11 void my_fun(char * p)
12 {
13 if(p == NULL)
14 return;
15
16 for(; *p != ''; p ++)
17 if(*p >= 'A' && p 1)
18 str = argv[1];
19
20 bzero(&sin, sizeof(sin));
21 sin.sin_family = AF_INET;
22 sin.sin_port = htons(port);
23 inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
24
25 sfd = socket(AF_INET, SOCK_STREAM, 0);
26
27 connect(sfd, (struct sockaddr_in *)&sin, sizeof(sin));
28
29 write(sfd, str, strlen(str) + 1);
30
31 read(sfd, buf, MAX_LINE);
32
33 printf("receive from server: %sn", buf);
34
35 close(sfd);
36
37 return 0;
38
}
waiting ...
client IP is 127.0.0.1, port is 59363
client is : Test String
receive from server: Test String
server.c
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8
9 #define MAX_LINE 100
10
11 void my_fun(char * p)
12 {
13 if(p == NULL)
14 return;
15
16 for(; *p != ''; p ++)
17 if(*p >= 'A' && p 1)
18 str = argv[1];
19
20 bzero(&sin, sizeof(sin));
21 sin.sin_family = AF_INET;
22 sin.sin_port = htons(port);
23 inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
24
25 sfd = socket(AF_INET, SOCK_STREAM, 0);
26
27 connect(sfd, (struct sockaddr_in *)&sin, sizeof(sin));
28
29 write(sfd, str, strlen(str) + 1);
30
31 read(sfd, buf, MAX_LINE);
32
33 printf("receive from server: %sn", buf);
34
35 close(sfd);
36
37 return 0;
38
}
|
1、大小写转化的代码有错:
if(*p >= 'A' && p
if(*p >= 'A' && p