写入套接字

时间:2014-10-02 08:08:15

标签: c sockets

我正在使用套接字创建一个简单的1对1服务器 - 客户端聊天应用程序。基本上有一个服务器可以同时与单个客户端通信。我想要做的是客户端应该发送一个字符串到服务器和服务器必须通过更改字符串的大小写将其发送回客户端(从上到下,反之亦然)。问题是字符串被发送到服务器,但服务器的响应从来没有考虑到客户端无法发送其他字符串。 该计划的输出: -

root @ User:〜/ Desktop / Aadil / SystemPracticum / Programs / Assignment5#。/Server 4000

来自客户的消息是message1

root @ User:〜/ Desktop / Aadil / SystemPracticum / Programs / Assignment5#./Client localhost 4000

输入消息message1

输入消息message2

谢谢 这是我的代码 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;
    }

1 个答案:

答案 0 :(得分: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)。