2014-10-02 103 views
-3

我正在使用套接字制作一个简单的1对1服务器 - 客户端聊天应用程序。基本上有一个服务器可以与单个客户端进行通信。我试图做的是客户端应该发送一个字符串到服务器和服务器必须发送它回到客户端与更改字符串的情况下(从上到下,反之亦然)。问题是字符串被发送到服务器,但从服务器的响应从未帐户该客户端无法发送其他字符串。 输出的程序: -写入套接字

根@用户:〜/桌面/ Aadil/SystemPracticum /程序/ Assignment5#./Server 4000

从客户端该消息是MESSAGE1

根@用户:〜 /桌面/ Aadil/SystemPracticum /程序/ Assignment5#./Client本地主机4000

输入消息MESSAGE1

输入消息的消息2

谢谢 这里是我的代码 Server.c

#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <string.h> 
#include <sys/types.h> 
#include <time.h> 
void ChangeCase(char *string){ 
    int i = 0; 
    while(string[i]){ 
     // printf("converting\n"); 
     if(string[i] <= 90 && string[i] >= 65) 
      string[i] += 32; 
     else 
      string[i] -= 32; 
     ++i; 
    } 

} 
int main(int counter, char *string[]){ 
    if(counter < 2){ 
     perror("erro! please provide port no.\n"); 
    }else{ 
     int server_socket_file_descriptor,client_socket_file_descriptor, 
     port_no,message_length,client_length; 
     char buffer[256];//buffer to be used for storing messages 
     struct sockaddr_in server_address,client_address; 

     server_socket_file_descriptor = socket(AF_INET,SOCK_STREAM,0); 
     /*it creates new socket the first argument AF_INET is used for internet domain 
      and second argument SOCK_STREAM is used for stream socket 
      third argument 0 means the default protocol for stram socket which is tcp*/ 
     if(server_socket_file_descriptor < 0) 
      perror("\t\t\t\t=====!!!cant create a socket!!!=====\n"); 

     bzero((char*)&server_address,sizeof(server_address));//set all value to 0 

     //set port no. by converting port from char* to integer    
     port_no = atoi(string[1]); 

     /*now initialize the server_address 
      server_address is a struct of sockaddr_in type which has four field in it 
      we need to initialize 3 of them 
     */ 
     server_address.sin_family = AF_INET; 
     //convert port no. to network byte order 
     server_address.sin_port = htons(port_no); 
     //set server ip address to the machines ip address in my case it is 10.8.3.236    
     server_address.sin_addr.s_addr=INADDR_ANY; 

     /*now we need to bind the server with socket created*/ 
     if(bind(server_socket_file_descriptor,(struct sockaddr*)&server_address, 
       sizeof(server_address)) < 0){ 
      perror("\t\t\t\t\t====error in binding====\n"); 
      return 0; 
     } 
     //since socket is bind correctly I am not checking for the error 
     listen(server_socket_file_descriptor,8); 
     /*listening to socket. 8 represent the maximum client that 
     can wait in queue to connect to the server*/ 

     //we are done with the server :D 

     client_length = sizeof(client_address); 
     client_socket_file_descriptor = accept(server_socket_file_descriptor, 
               (struct sockaddr*)&client_address, 
               &client_length); 
     if(client_socket_file_descriptor < 0) 
      perror("\t\t\t\t unable to connect to client"); 

     while(1){ 
      bzero(buffer,256); 
      message_length = read(client_socket_file_descriptor,buffer,255); 

      if(message_length < 0) 
       perror("\t\t\t\t error in reading from socket\n"); 

      printf("\t\t\t\tthe message from client is %s\n",buffer); 
      ChangeCase(buffer); 

      message_length = write(client_socket_file_descriptor,buffer,sizeof(buffer)); 
      if(message_length < 0) 
       perror("\t\t\t\t error writing to socket\n"); 
     } 
     return 0; 
    } 
} 

Client.c

#include <sys/socket.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <errno.h> 
#include <arpa/inet.h> 

    int main(int argc, char *argv[]) 
    { 
     int file_descriptor,message_length,port_no; 
     char Buffer[256];//to store the message 
     //to store the address of the server to  which  we want to connect 
     struct sockaddr_in server_address; 
     struct hostent *server;//hostent defines the host computer on internet 
     if(argc < 3){ 
      printf("\t\t\t please provide ip address and port no.\n"); 
      return 1; 
     } 
     port_no = atoi(argv[2]); 

     if((file_descriptor = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
     { 
      printf("\n Error : Could not create socket \n"); 
      return 1; 
     } 

     memset(&server_address, '0', sizeof(server_address)); 

     server_address.sin_family = AF_INET; 
     //convert port no. to network byte order 
     server_address.sin_port = htons(port_no); 
     //set server ip address to the machines ip address in my case it is 10.8.3.236 
     server_address.sin_addr.s_addr = INADDR_ANY; 

     if(connect(file_descriptor,(struct sockaddr*)&server_address, 
            sizeof(server_address))<0){ 
      perror("error in connection\n"); 
      return 1; 
     } 

     while(1){ 
      memset(Buffer, '0',sizeof(Buffer)); 
      printf("\t\t\t\t\tenter the message\n"); 
      fgets(Buffer,255,stdin); 
      message_length = write(file_descriptor,Buffer,strlen(Buffer)); 

      if(message_length<0) 
       perror("\t\t\t\terror in writing\n"); 

      memset(Buffer,'0',sizeof(Buffer)); 
      message_length = read(file_descriptor,Buffer,255); 

      if(message_length < 0) 
       perror("\t\t\terror in reading from buffer\n"); 
      else{ 
       printf("%s\n",Buffer); 
      } 
     } 

     return 0; 
    } 
+0

会发生什么?从服务器和客户端的任何输出?您可以添加更多的调试printf,以显示连接构建的哪些步骤已成功(在服务器端进行绑定,侦听,接受;在客户端进行连接)。 – 2014-10-02 08:13:22

+0

@Aadil Ahmad请粘贴客户端和服务器的输出 – 4pie0 2014-10-02 08:14:34

+1

@Aadil Ahmad pleeeeaaase,不要在评论中提供额外的信息,编辑您的问题! – zoska 2014-10-02 08:43:30

回答

1

我怀疑问题位于这条线在你的服务器代码:

message_length = write(client_socket_file_descriptor,buffer,sizeof(buffer)); 

请注意,此行始终将256个字节发送回客户端。对于像“message1”这样的字符串,这意味着它将发回“MESSAGE1”,然后是248 NUL /零字节。根据TCP堆栈决定如何分解这些字节,客户端的read()调用可能会接收到不同部分序列中的那些字节,并且如果接收到的任何部分序列以NUL /零字节开始,它将打印作为一个空字符串出来。

为了更好地看到发生了什么事情,你可能会取代这条线在你的客户端:

printf("%s\n",Buffer); 

像这样的东西:

printf("[%s]\n",Buffer); 

我也建议你改变你的服务器指定strlen(buffer)作为write()的最终参数而不是sizeof(buffer)。