2013-10-15 246 views
2

我是新來的套接字編程和C以及。我正在創建一個簡單的echo客戶端服務器應用程序,它的運行方式如下:如果客戶端發送「hi」----服務器recv「wellcome」
如果客戶端發送「bye」----- server recv「看你的」回聲客戶端服務器在C

否則,如果我發送的任何其他字符串我將RECV我所發**

這裏是我的服務器:

#include<stdio.h> 
#include<string.h> //strlen 
#include<sys/socket.h> 
#include<arpa/inet.h> //inet_addr 
#include<unistd.h> //write 

int main(int argc , char *argv[]) 
{ 
    int socket_desc , client_sock , c , read_size; 
    struct sockaddr_in server , client; 
    char client_message[2000]; 
    char wellcome[]="wellcome", seeyou[]="see you",hi[]="hi",bye[]="bye"; 

    //Create socket 
    socket_desc = socket(AF_INET , SOCK_STREAM , 0); 
    if (socket_desc == -1) 
    { 
     printf("Could not create socket"); 
    } 
    puts("Socket created"); 

    //Prepare the sockaddr_in structure 
    server.sin_family = AF_INET; 
    server.sin_addr.s_addr = INADDR_ANY; 
    server.sin_port = htons(8882); 

    //Bind 
    if(bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) 
    { 
     //print the error message 
     printf("bind failed. Error"); 
     return 1; 
    } 
    puts("bind done"); 

    //Listen 
    listen(socket_desc , 3); 

    //Accept and incoming connection 
    puts("Waiting for incoming connections..."); 
    c = sizeof(struct sockaddr_in); 

    //accept connection from an incoming client 
    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c); 
    if (client_sock < 0) 
    { 
     printf("accept failed"); 
     return 1; 
    } 
    puts("Connection accepted"); 

    //Receive a message from client 
    while((read_size = recv(client_sock , client_message , 2000 , 0)) > 0) 
    { 
     //Send the message back to client 
     *(client_message + read_size) = '\0'; 
    //*(wellcome + read_size) = '\0'; 
    //*(seeyou + read_size) = '\0'; 
    if(strcmp(client_message,hi)==0) 
     write(client_sock, wellcome, strlen(wellcome)); 
    else 
     if (strcmp(client_message,bye)==0) 
      write(client_sock, seeyou, strlen(seeyou)); 


     write(client_sock , client_message , strlen(client_message)); 
    } 

    if(read_size == 0) 
    { 
     puts("Client disconnected"); 
     fflush(stdout); 
    } 
    else if(read_size == -1) 
    { 
     printf("recv failed"); 
    } 

    return 0; 
} 

和客戶端:

#include<stdio.h> 
#include<string.h> 
#include<sys/socket.h> 
#include<arpa/inet.h> 

int main(int argc , char *argv[]) 
{ 
    int sock; 
    struct sockaddr_in server; 
    char message[1000] , server_reply[2000]; 

    //Create socket 
    sock = socket(AF_INET , SOCK_STREAM , 0); 
    if (sock == -1) 
    { 
     printf("cound'n create socket"); 
    } 
    puts("Socket created"); 

    server.sin_addr.s_addr = inet_addr("127.0.0.1"); 
    server.sin_family = AF_INET; 
    server.sin_port = htons(8792); 

    //Connect to remote server 
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) 
    { 
     printf("connection error"); 
     return 1; 
    } 

    puts("Connected\n"); 

    //keep communicating with server 
    while(1) 
    { 
     printf("write your message : "); 
     gets(message); 

     //Send some data 
     if(send(sock , message , strlen(message) , 0) < 0) 
     { 
      puts("send error"); 
      return 1; 
     } 

     //Receive a reply from the server 
    bzero(server_reply,2000); 
     if(recv(sock , server_reply , 2000 , 0) < 0) 
     { 
      puts("recv failed"); 
      break; 
     } 

     puts("Server reply :"); 
     puts(server_reply); 
    } 

    close(sock); 
    return 0; 
} 

,我跑它 服務器:

[[email protected] chat]# gcc -o newser newser.c 
[[email protected] chat]# ./newser 
Socket created 
bind done 
Waiting for incoming connections... 
Connection accepted 

,這裏是與客戶端的問題:

[[email protected] chat]# ./client 127.0.0.1 
Socket created 
Connected 

write your message : hi 
Server reply : 
wellcome 
write your message : bye 
Server reply : 
hi 
write your message : bye 
Server reply : 
see youbye 
write your message : hey buddy 
Server reply : 
see youbye 
write your message : hey ya 
Server reply : 
hey buddy 
write your message : 

你可以看到:如果我打招呼,服務器回覆惠康。 如果我說再見,服務器回覆嗨。 如果我說再見增益,服務器回覆看到你(它不是我期待的)。

回答

1

在服務器代碼:

if(strcmp(client_message,hi)==0) 
    write(client_sock, wellcome, strlen(wellcome)); 
else if (strcmp(client_message,bye)==0) 
    write(client_sock, seeyou, strlen(seeyou)); 
#You don't want to do this for above 2 cases so add else 
else 
    write(client_sock , client_message , strlen(client_message)); 
+0

我想你的方式,但它不能正常工作。無論如何謝謝你! –

2

我認爲,問題就在這裏:

write(client_sock , client_message , strlen(client_message)); 

爲什麼你發送客戶端消息以客戶端?

另一個問題可能來自部分寫入:檢查寫入調用的返回值。

而且請用fgets代替get。見獲取(3)。