LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 519|回复: 2

这一对C/S程序有问题么?

[复制链接]
发表于 2006-4-24 21:40:14 | 显示全部楼层 |阅读模式
这段程序的问题是客户端能收到并输出服务端的的返回信息字符串。但是服务端能收到却不能显示客户端发来的字符串.

server端是过去装的debian stable
client端是新装的ebian testing

  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>

  6. int port =8000;

  7. void main(){
  8. struct sockaddr_in sin;
  9. struct sockaddr_in pin;
  10. int sock_descriptor;
  11. int temp_sock_descriptor;
  12. int address_size;
  13. char buf[16384];
  14. int i,len;

  15. sock_descriptor = socket(AF_INET,SOCK_STREAM,0);
  16. if(sock_descriptor == -1){
  17.    perror("call to socket");
  18. }
  19. bzero(&sin,sizeof(sin));
  20. sin.sin_family = AF_INET;
  21. sin.sin_addr.s_addr = INADDR_ANY;
  22. sin.sin_port = htons(port);

  23. if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin)) == -1){
  24.   perror("call to bind");
  25.   exit(1);
  26. }
  27. if (listen(sock_descriptor,20) == -1){
  28.    perror("call to listen");
  29.    exit(1);
  30. }
  31. printf("Accepting connection ...\n");

  32. while(1){
  33. temp_sock_descriptor = accept(sock_descriptor, (struct sockaddr *)&pin,&address_size);

  34. if(temp_sock_descriptor == -1){
  35.   perror("call to accept");
  36.   exit(1);
  37. }
  38. if(recv(temp_sock_descriptor,buf,16384,0) == -1){
  39.   perror("call to recv");
  40.   exit(1);
  41. }
  42. printf("received from client:%s\n",buf);


  43. len = strlen("OK.OK.OK.OK!");

  44. if(send(temp_sock_descriptor,"OK,OK,OK,OK!",len,0) == -1){
  45.   perror("call to send");
  46.   exit(1);
  47. }
  48. close(temp_sock_descriptor);
  49. }
  50. }
复制代码




client:
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>

  6. char *host_name = "192.168.0.101";
  7. int port = 8000;

  8. void main(int argc,char *argv[]){
  9.      char buf[8192];
  10.      char message[256];
  11.      int socket_descriptor;
  12.      struct sockaddr_in pin;
  13.      struct hostent *server_host_name;

  14. char *str = "A default test string";

  15. if(argc < 2){
  16.   printf("Usage:'clinet "Any test string"\n");
  17.   printf("We will send a default test string.\n");
  18. } else{
  19. str=argv[1];
  20. }
  21. if((server_host_name = gethostbyname(host_name)) ==0){
  22.   perror("Error resolving local host\n");
  23.   exit(1);
  24. }
  25. bzero(&pin,sizeof(pin));
  26. pin.sin_family = AF_INET;
  27. pin.sin_addr.s_addr = htonl(INADDR_ANY);
  28. pin.sin_addr.s_addr = ((struct in_addr *) (server_host_name->h_addr))->s_addr;
  29. pin.sin_port = htons(port);

  30. if((socket_descriptor = socket(AF_INET,SOCK_STREAM,0)) == -1){
  31.   perror("Error opening socket\n");
  32.   exit(1);
  33. }
  34. if(connect(socket_descriptor,(void *)&pin,sizeof(pin)) == -1){
  35.   perror("Error connecting to socket\n");
  36.   exit(1);
  37. }
  38. printf("Sending message %s to server...\n",str);

  39. if(send(socket_descriptor,buf,8192,0) == -1){
  40.   perror("Error in send\n");
  41.   exit(1);
  42. }
  43. printf("..sent message.. wait for response..\n");

  44. if(recv(socket_descriptor,buf,8192,0) == -1){
  45.   perror("Error in receiving response from server\n");
  46.   exit(1);
  47. }
  48. printf("\n Response from server:\n\n%s\n",buf);
  49. close(socket_descriptor);
  50. }

复制代码
发表于 2006-4-24 22:13:45 | 显示全部楼层
服务器端接收的数据没有NULL结尾
if(recv(temp_sock_descriptor,buf,16384,0) == -1){
  perror("call to recv");
  exit(1);
}
printf("received from client:%s\n",buf);
你用printf("%s",buf)当然打印会出问题的,改成这样的试试:
int bytes;
if((bytes = recv(temp_sock_descriptor,buf,16383,0) ) < 0){
  perror("call to recv");
  exit(1);
}
buf[bytes] = '\0';
printf("received from client:%s\n",buf);
回复 支持 反对

使用道具 举报

发表于 2006-4-24 22:20:57 | 显示全部楼层
你的客户端程序的
if(send(socket_descriptor,buf,8192,0) == -1){
  perror("Error in send\n");
  exit(1);
}
中的buf好像什么东西都没有哦!!而且还有很多没有用的东西
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表