-3
我正在通過在它們之間建立套接字連接來在一個客戶端和服務器之間執行這個簡單的聊天應用程序。但它正在無限循環中。任何人都可以說,什麼是錯誤,我已經做了...C中的服務器客戶端聊天應用程序
客戶端代碼
int main(){
int client_socket = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(client_socket, (struct sockaddr*) &server_address, sizeof(server_address));
char server_response[256];
while(1){
read(client_socket, server_response, sizeof(server_response));
printf("Server:%s\n",server_response);
printf("Client:");
write(client_socket, server_response, sizeof(server_response));
if(strncmp("exit",server_response,4)==0){
printf("Server Exit Successful");
break;
}
}
close(client_socket);
return 0;
}
服務器代碼
int main(){
char server_message[256];
int server_socket = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in server_address;
server_address.sin_family=AF_INET;
server_address.sin_port=htons(9002);
server_address.sin_addr.s_addr=INADDR_ANY;
bind(server_socket,(struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket,1);
int network_socket = accept(server_socket,NULL,NULL);
while(1){
write(network_socket,server_message,sizeof(server_message));
printf("Client:%s\n",server_message);
printf("Server:");
read(network_socket,server_message,sizeof(server_message));
if((strncmp(server_message,"exit",4))==0)
{
printf("Client Exit...\n");
break;
}
}
close(server_socket);
return 0;
}
「*什麼是錯誤*」是一個,你錯過了顯示相關的代碼。因此,問題很可能會很快收到downvotes和結束... – alk
鑑於可用的信息,沒有。 –
發佈一些代碼,請 –