
服务器
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
int client_num = 0;
const int max_client = 10;
int client_fd[10];
int sock_fd;
pthread_t tid[10];
char userinfo[10][3][100];
void server_accept(void);
void server_broadcast(void* sid);
void func(void);
int main(int argc, char const *argv[])
{
signal(SIGINT, (void *)func);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1)
{
perror("socket");
exit(1);
}
printf("socket create success\n");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8848);
addr.sin_addr.s_addr = inet_addr("192.168.124.72");
int ret = bind(sock_fd, (const struct sockaddr *)&addr, sizeof(addr));
if (ret == -1)
{
perror("bind");
exit(1);
}
printf("server started\n");
ret = listen(sock_fd, max_client);
if (ret == -1)
{
perror("listen");
exit(1);
}
printf("server listening\n");
pthread_t id;
pthread_create(&id, NULL, (void *)server_accept, NULL);
pause();
close(sock_fd);
return 0;
}
void server_accept(void)
{
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
while (1)
{
client_fd[client_num] = accept(sock_fd, (struct sockaddr *)&client_addr, &client_addr_len);
snprintf(userinfo[client_num][0], sizeof(userinfo[client_num][0]), "%d", client_fd[client_num]);
snprintf(userinfo[client_num][1], sizeof(userinfo[client_num][1]), "%s", inet_ntoa(client_addr.sin_addr));
snprintf(userinfo[client_num][2], sizeof(userinfo[client_num][2]), "%d", ntohs(client_addr.sin_port));
if (client_fd[client_num] >= 0)
{
int sid=client_num;
printf("client%d connected\n", client_fd[client_num]);
pthread_create(&tid[client_num], NULL, (void *)server_broadcast, &sid);
client_num++;
}
else
{
perror("accept");
exit(1);
}
if (client_num >= max_client)
{
break;
}
}
}
void server_broadcast(void* sid)
{
int id = *((int *)sid);
int fd = client_fd[id];
printf("client%d,ip:%s,port:%s\n", fd, userinfo[id][1],userinfo[id][2]);
char buf[1000];
while (1)
{
memset(buf, 0, sizeof(buf));
int ret = recv(fd, buf, sizeof(buf), 0);
if (ret == -1)
{
perror("read");
exit(1);
}
else if (ret == 0)
{
printf("client%d closed\n", fd);
close(fd);
client_num--;
break;
}
printf("%s:%s-->client%d say:%s\n",userinfo[id][1],userinfo[id][2],fd, buf);
for (int i = 0; i < client_num; i++)
{
if(client_fd[i]==fd) continue;
send(client_fd[i], buf, strlen(buf), 0);
}
}
}
void func(void)
{
close(sock_fd);
for (int i = 0; i < max_client; i++)
{
if ((client_fd[i] >= 0) && (tid[i]!= 0))
{
close(client_fd[i]);
pthread_cancel(tid[i]);
}
}
exit(0);
}
客户端
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int sock_fd;
void clent_recv(void);
int main(int argc, char const *argv[])
{
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1)
{
perror("socket");
exit(1);
}
printf("socket create success\n");
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(8848);
addr.sin_addr.s_addr=inet_addr("192.168.124.72");
int ret = connect(sock_fd,(const struct sockaddr *)&addr,sizeof(addr));
if(ret == -1)
{
perror("connect");
exit(1);
}
printf("connect success\n");
pthread_t pid;
pthread_create(&pid,NULL,(void *)clent_recv,NULL);
char name[20];
printf("输入你的名字\n");
fgets(name,20,stdin);
char s_buf[1024];
char buf[1000];
while (1)
{
memset(s_buf,0,sizeof(s_buf));
memset(buf,0,sizeof(buf));
printf("请输入要发送的内容:\n");
fgets(buf,sizeof(buf),stdin);
strncpy(s_buf,name,strlen(name)-1);
strcat(s_buf,":");
strcat(s_buf,buf);
ret = send(sock_fd,s_buf,strlen(s_buf)-1,0);
if (ret == -1)
{
perror("send");
exit(1);
}
}
close(sock_fd);
return 0;
}
void clent_recv(void)
{
char buf[1024];
while(1)
{
memset(buf,0,sizeof(buf));
int ret = recv(sock_fd,buf,sizeof(buf),0);
if (ret == -1)
{
printf("read error\n");
perror("read");
exit(1);
}
else if (ret == 0)
{
printf("client closed\n");
break;
}
printf("%s\n", buf);
}
}