我已经使用 C 中的 Socket 编程修改了一个简单的聊天程序代码。我的问题是我必须在服务器或客户端输入两行才能接收另一行。我已经上传了一张图片来解释更多。任何帮助表示赞赏
SERVER
#include <stdio.h>
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<strings.h>
#include<unistd.h>
#define SERVER_TCP_PORT 3000
#define BUFLEN 256
int main (int argc,char **argv)
{
int n,bytes_to_read;
int sd,new_sd,client_len,port;
struct sockaddr_in server,client;
char *bp,buf[BUFLEN];
switch (argc) {
case 1:
port = SERVER_TCP_PORT;
break;
case 2:
port = atoi (argv[1]);
break;
default:
fprintf (stderr,"Usage: %s[port]\n",argv[0]);
exit(1);
}
/*Create a system socket */
if ((sd = socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Can't create a socket\n");
exit(1);
}
/*Bind an address to the socket*/
bzero ((char*)&server,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sd,(struct sockaddr *)&server,sizeof(server))==-1){
fprintf(stderr,"can't bind name to socket\n");
exit(1);
}
/*queue up to 5 connect requests*/
listen(sd,5);
while(1){
client_len = sizeof(client);
if((new_sd = accept(sd,(struct sockaddr *)&client,&client_len))==-1){
fprintf(stderr,"can't accept client \n");
exit(1);
}
if(fork())
{
printf("\n Server listening to clients on port 2000");
printf("\n Server got request from%s",inet_ntoa(client.sin_addr));
printf("\n SEND DATA<q or Q to quit> :%s",buf);
scanf("%s",buf);
while((strcmp(buf,"q")!=0)&&(strcmp(buf,"Q")!=0))
{
send(new_sd,buf,30,0);
scanf("%s",buf);
}
send(new_sd,"q",5,0);
}
else{
n = recv(new_sd,buf,30,0);
while((strcmp(buf,"q")!=0)&&(strcmp(buf,"Q")!=0))
{
printf("\nRECEIVED DATA =%s",buf);
n = recv(new_sd,buf,30,0);
}
printf("\n Bye");
send(new_sd,"EOF",4,0);
close(sd);
close(new_sd);
}
}
return 0;
}
Client
#include <stdio.h>
#include <sys/types.h>
#include<sys/socket.h>
#include <netdb.h>
#include<stdlib.h>
#include<strings.h>
#include <netinet/in.h>
#define SERVER_TCP_PORT 3000
#define BUFLEN 256
int main(int argc,char **argv)
{
int n,bytes_to_read;
int sd,port;
struct hostent *hp;
struct sockaddr_in server;
char *host,*bp,rbuf[BUFLEN],sbuf[BUFLEN];
switch(argc) {
case 2:
host = argv[1];
port = SERVER_TCP_PORT;
break;
case 3:
host = argv[1];
port = atoi(argv[2]);
break;
default:
fprintf(stderr,"Usage: %s host[port] \n",argv[0]);
exit(1);
}
/*Create a stream socket*/
if ((sd=socket(AF_INET,SOCK_STREAM,0))==-1){
fprintf(stderr,"Can't create a socket\n");
exit(1);
}
bzero ((char *)&server,sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(port);
if((hp = gethostbyname(host))==NULL){
fprintf (stderr,"Can't get server's address\n");
exit(1);
}
bcopy(hp->h_addr,(char *)&server.sin_addr,hp->h_length);
/*connecting to the server*/
if (connect (sd,(struct sockaddr *)&server,sizeof(server))==-1){
fprintf(stderr,"Can't connect \n");
exit(1);
}
printf("Connected:server's address is %s\n",hp->h_name);
if(fork())
{
bzero(rbuf,30);
printf("\n SEND DATA<q or Q to quit> :%s",rbuf);
scanf("%s",rbuf);
while((strcmp(rbuf,"q")!=0)&&(strcmp(rbuf,"Q")!=0))
{
send(sd,rbuf,30,0);
scanf("%s",rbuf);
}
send(sd,"q",5,0);
}
else{
n = recv(sd,rbuf,30,0);
while((strcmp(rbuf,"q")!=0)&&(strcmp(rbuf,"Q")!=0))
{
printf("\nRECEIVED DATA =%s",rbuf);
n = recv(sd,rbuf,30,0);
}
printf("\n Bye");
send(sd,"EOF",4,0);
close(sd);
}
return 0;
}