我正在使用套接字製作一個簡單的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;
}
會發生什麼?從服務器和客戶端的任何輸出?您可以添加更多的調試printf,以顯示連接構建的哪些步驟已成功(在服務器端進行綁定,偵聽,接受;在客戶端進行連接)。 – 2014-10-02 08:13:22
@Aadil Ahmad請粘貼客戶端和服務器的輸出 – 4pie0 2014-10-02 08:14:34
@Aadil Ahmad pleeeeaaase,不要在評論中提供額外的信息,編輯您的問題! – zoska 2014-10-02 08:43:30