socket网络编程
一、实现双方聊天
1.服务端代码(server.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
*/
int main(int argc,char**argv)
{
int server_id;
int client_id;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char read_buffer[128];
char *msg = "Received,poorguy of message";
if(argc != 3)
{
printf("invaid parameter\n");
return -1;
}
memset(&server_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
server_id = socket(AF_INET,SOCK_STREAM,0);
if(server_id == -1)
{
printf("create socket failed\n");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&server_addr.sin_addr);
if( bind(server_id,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1 )
{
printf("bind error\n");
}
listen(server_id,10);
int client_len = sizeof(struct sockaddr);
while(1)
{
client_id = accept(server_id,(struct sockaddr *)&client_addr,&client_len);
if( client_id == -1 )
{
printf("connect failed\n");
}
printf("get connect:%s\n",inet_ntoa(client_addr.sin_addr));
if(fork() == 0)
{
memset(read_buffer,0,sizeof(read_buffer));
int read_byte = read(client_id,read_buffer,128);
if(read_byte == -1 )
{
printf("read message failed\n");
}
printf("From client message:%s\n",read_buffer);
int write_byte = write(client_id,msg,strlen(msg));
if(write_byte==-1)
{
printf("send message fail\n");
}
break;
}
}
return 0;
}
2.客户端代码(client.c)
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
int main(int argc,char**argv)
{
int client_id;
struct sockaddr_in client_addr;
char read_buffer[128];
char msg[128];
if(argc != 3)
{
printf("invaid parameter\n");
return -1;
}
memset(&client_addr,0,sizeof(struct sockaddr_in));
client_id = socket(AF_INET,SOCK_STREAM,0);
if(client_id == -1)
{
printf("create socket failed\n");
return -1;
}
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&client_addr.sin_addr);
if ( connect(client_id,(struct sockaddr *)&client_addr,sizeof(struct sockaddr_in)) == -1 )
{
printf("connect fail\n");
}
printf("input data:");
gets(msg);
if( write(client_id,msg,strlen(msg)) == -1)
{
printf("send message failed\n");
}
memset(read_buffer,0,sizeof(read_buffer));
if( read(client_id,read_buffer,sizeof(read_buffer)) == -1)
{
printf("read message failed\n");
}
printf("From server message:%s\n",read_buffer);
return 0;
}
3.效果截图
二、多客户端互相聊天
1.多人聊天服务端代码(server_pro.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
int main(int argc,char**argv)
{
int server_id;
int client_id;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
char read_buffer[128];
char *msg = "Received,poorguy of message";
if(argc != 3)
{
printf("invaid parameter\n");
return -1;
}
memset(&server_addr,0,sizeof(struct sockaddr_in));
memset(&client_addr,0,sizeof(struct sockaddr_in));
server_id = socket(AF_INET,SOCK_STREAM,0);
if(server_id == -1)
{
printf("create socket failed\n");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&server_addr.sin_addr);
if( bind(server_id,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1 )
{
printf("bind error\n");
}
listen(server_id,10);
int client_len = sizeof(struct sockaddr);
int count = 0;
while(1)
{
client_id = accept(server_id,(struct sockaddr *)&client_addr,&client_len);
if( client_id == -1 )
{
printf("connect failed\n");
}
printf("get connect:%s\n",inet_ntoa(client_addr.sin_addr));
count++;
if(fork() == 0)
{
if(fork() == 0)
{
while(1)
{
sprintf(msg,"欢迎第%d个客户端加入聊天室\n",count);
int write_byte = write(client_id,msg,strlen(msg));
sleep(5);
}
}
while(1)
{
memset(read_buffer,0,sizeof(read_buffer));
int read_byte = read(client_id,read_buffer,128);
if(read_byte == -1 )
{
printf("read message failed\n");
}
printf("From NO.%d message:%s\n",count,read_buffer);
if(strcmp(read_buffer,"quit") == 0)
{
printf("NO.%d client quit chatroom\n",count);
break;
}
}
}
}
close(server_id);
close(client_id);
return 0;
}
2.多人聊天客户端代码(client_pro.c)
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
int main(int argc,char**argv)
{
int client_id;
struct sockaddr_in client_addr;
char read_buffer[128];
char msg[128];
if(argc != 3)
{
printf("invaid parameter\n");
return -1;
}
memset(&client_addr,0,sizeof(struct sockaddr_in));
client_id = socket(AF_INET,SOCK_STREAM,0);
if(client_id == -1)
{
printf("create socket failed\n");
return -1;
}
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&client_addr.sin_addr);
if ( connect(client_id,(struct sockaddr *)&client_addr,sizeof(struct sockaddr_in)) == -1 )
{
printf("connect fail\n");
}
while(1)
{
if(fork() == 0)
{
while(1)
{
printf("input data:");
gets(msg);
if( write(client_id,msg,strlen(msg)) == -1)
{
printf("send message failed\n");
}
}
}
while(1)
{
memset(read_buffer,0,sizeof(read_buffer));
if( read(client_id,read_buffer,sizeof(read_buffer)) == -1)
{
printf("read message failed\n");
}
printf("From server message:%s\n",read_buffer);
}
}
return 0;
}
3.多人聊天效果截图