C、Socket编程:1个服务器2个客户端通过HUB连接,使用TCP的聊天应用程序
我有用于在 1 个服务器 1 个客户端之间进行连接和聊天的代码,如下所示:
/*Server Side*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int sd,i,len,bi,nsd,port;
char content[30];
struct sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\nSocket problem");
return 0;
}
printf("\nSocket created\n");
bzero((char*)&cli,sizeof(ser));
printf("ENTER PORT NO:\n");
scanf("%d",&port);
printf("\nPort Address is %d\n:",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser));
if(bi==-1)
{
printf("\nBind error, Port busy, Plz change port in client and server");
return 0;
}
i=sizeof(cli);
listen(sd,5);
nsd = accept(sd,((struct sockaddr *)&cli),&i);
if(nsd==-1)
{
printf("\nCheck the description parameter\n");
return 0;
}
printf("\nConnection accepted!");
if(fork())
{
printf("\nEnter the data to be send type exit for stop:\n");
scanf("%s",content);
while(strcmp(content,"exit")!=0)
{
send(nsd,content,30,0);
scanf("%s",content);
}
send(nsd,"exit",5,0);
}
else
i = recv(nsd,content,30,0);
while(strcmp(content,"exit")!=0)
{
printf("\nClient: %s\n",content);
i=recv(nsd,content,30,0);
}
printf("\nBye");
send(nsd,"Offline",10,0);
close(sd);
close(nsd);
return 0;
}
/*Client Side*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int sd,con,port,i,Res;
char content[30];
struct sockaddr_in cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\nSocket problem");
return 0;
}
bzero((char*)&cli,sizeof(cli));
cli.sin_family = AF_INET;
printf("ENTER PORT NO:\n");
scanf("%d",&port);
cli.sin_port=htons(port);
cli.sin_addr.s_addr=htonl(INADDR_ANY);
con=connect(sd,(struct sockaddr*)&cli,sizeof(cli));
if(con==-1)
{
printf("\nConnection error");
return 0;
}
if(fork())
{
printf("\nEnter the data to be send type exit for stop:\n");
scanf("%s",content);
while(strcmp(content,"exit")!=0)
{
send(sd,content,30,0);
scanf("%s",content);
}
send(sd,"exit",5,0);
}
else
{
i=recv(sd,content,30,0);
while(strcmp(content,"exit")!=0)
{
printf("\nServer: %s\n",content);
i=recv(sd,content,30,0);
}
send(sd,"exit",5,0);
}
close(sd);
return 0;
}
我需要连接另一个客户端,该客户端也可以使用同一端口进行聊天。 请给我代码来做到这一点。 谢谢。
I have codes for connecting and Chatting between 1 Server 1 Client as follows:
/*Server Side*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int sd,i,len,bi,nsd,port;
char content[30];
struct sockaddr_in ser,cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\nSocket problem");
return 0;
}
printf("\nSocket created\n");
bzero((char*)&cli,sizeof(ser));
printf("ENTER PORT NO:\n");
scanf("%d",&port);
printf("\nPort Address is %d\n:",port);
ser.sin_family=AF_INET;
ser.sin_port=htons(port);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
bi=bind(sd,(struct sockaddr *)&ser,sizeof(ser));
if(bi==-1)
{
printf("\nBind error, Port busy, Plz change port in client and server");
return 0;
}
i=sizeof(cli);
listen(sd,5);
nsd = accept(sd,((struct sockaddr *)&cli),&i);
if(nsd==-1)
{
printf("\nCheck the description parameter\n");
return 0;
}
printf("\nConnection accepted!");
if(fork())
{
printf("\nEnter the data to be send type exit for stop:\n");
scanf("%s",content);
while(strcmp(content,"exit")!=0)
{
send(nsd,content,30,0);
scanf("%s",content);
}
send(nsd,"exit",5,0);
}
else
i = recv(nsd,content,30,0);
while(strcmp(content,"exit")!=0)
{
printf("\nClient: %s\n",content);
i=recv(nsd,content,30,0);
}
printf("\nBye");
send(nsd,"Offline",10,0);
close(sd);
close(nsd);
return 0;
}
/*Client Side*/
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int sd,con,port,i,Res;
char content[30];
struct sockaddr_in cli;
if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("\nSocket problem");
return 0;
}
bzero((char*)&cli,sizeof(cli));
cli.sin_family = AF_INET;
printf("ENTER PORT NO:\n");
scanf("%d",&port);
cli.sin_port=htons(port);
cli.sin_addr.s_addr=htonl(INADDR_ANY);
con=connect(sd,(struct sockaddr*)&cli,sizeof(cli));
if(con==-1)
{
printf("\nConnection error");
return 0;
}
if(fork())
{
printf("\nEnter the data to be send type exit for stop:\n");
scanf("%s",content);
while(strcmp(content,"exit")!=0)
{
send(sd,content,30,0);
scanf("%s",content);
}
send(sd,"exit",5,0);
}
else
{
i=recv(sd,content,30,0);
while(strcmp(content,"exit")!=0)
{
printf("\nServer: %s\n",content);
i=recv(sd,content,30,0);
}
send(sd,"exit",5,0);
}
close(sd);
return 0;
}
I need to connect another client which can also chat using the same port.
Please give me codes to do that.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用
fork()
后,您必须分叉服务器的程序逻辑。一个分支与客户端通信,另一个分支必须再次调用accept。无需生成新进程,您还可以使用
select()
函数处理所有连接以及监听进程。该函数的结果和 FD_ISSET 宏的结果将指示需要处理或建立哪个连接。You must fork your server's program logic after calling
fork()
. One branch communicates with the client, the other will have to call accept again.There is no need to spawn a new process, you can also handle all connections as well as the listen process with the
select()
function. The result of that function and the result of the FD_ISSET macros will indicate, which connection needs to be handled or established.我们可以在服务器程序中使用线程连接多个客户端。为此,我们需要在服务器中使用 pthread 头文件。
we can connect more than 1 client using thread in server program. for that we need to use pthread header file in server.