此客户端和服务器程序成功编译。运行它等待客户端连接的服务器。 但是当我运行这个客户端程序时,它总是显示"连接失败..." 我不明白为什么......
server.c
int main()
{
struct sockaddr_in main_server, cli_1_server, cli_2_server;
int main_sock, cli_1_sock, cli_2_sock;
int Len;
char arv_msg[MAXSZ];
//char client_name[20];
// FILE *fp;
//short int RcvByte_1 = 0, RcvByte_2 = 0;
if( ( main_sock = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
{
printf("socket creation failed...\n");
exit(-1);
}
main_server.sin_family = AF_INET;
main_server.sin_addr.s_addr = inet_addr("127.0.0.1");
main_server.sin_port = htons(10000);
if( ( bind( main_sock, (SA *)&main_server, sizeof(main_server) ) ) < 0)
{
printf("binding failed...\n");
exit(-1);
}
printf("Listining...\n");
listen(main_sock, WAITING);
Len = sizeof(cli_1_server);
if( ( cli_1_sock = accept( main_sock, (SA *)&cli_1_server, &Len ) ) <0)
{
printf("Client 1 request failed...\n");
exit(-1);
}
printf("client 1 connected...\n");
}
client.c
int main()
{
struct sockaddr_in main_server;
int cli_1_sock;
char snd_msg[MAXSZ], rcv_msg[MAXSZ];
int n = 0;
if( ( cli_1_sock = (AF_INET, SOCK_STREAM, 0) ) == -1 )
{
printf("socket creation failed...\n");
exit(-1);
}
main_server.sin_family = AF_INET;
main_server.sin_addr.s_addr = inet_addr("127.0.0.1");
main_server.sin_port = htons(10000);
if( ( connect(cli_1_sock, (SA *)&main_server,sizeof(main_server)))==-1)
{
printf("connection failed...\n");
exit(-1);
}
printf("connected\n");
}
这是我的服务器和client..program
答案 0 :(得分:0)
更改
cli_1_sock = (AF_INET, SOCK_STREAM, 0)
到
cli_1_sock = socket(AF_INET, SOCK_STREAM, 0)