【linux】网络聊天室Socket编程
这是18年7月写的,拿来填充一下😜
linux下的网络聊天室,服务端,c客户端,java客户端,Android端 基于TCP开发 源代码 附有udp socket 服务端 客户端
** 下载链接**
服务器端
1void SendMsg(char *msg){
2 int i;
3 char tempmsg[286];
4 strcpy(tempmsg, msg);
5 char nowtime[10];
6 char sendbuffer[500] = {}; //最终消息发送
7 time_t t;
8 struct tm *lt;
9 time(&t);
10 lt = localtime(&t);
11 sprintf(nowtime, "[%d:%d:%d]", lt->tm_hour, lt->tm_min, lt->tm_sec);
12
13 strcat(sendbuffer, nowtime);
14 strcat(sendbuffer, tempmsg);
15 for (i = 0; i < MAXNUM; ++i)
16 {
17 if (clientSock[i] != 0)
18 {
19 printf("发送至客户[ %d ]\n", clientSock[i]);
20 send(clientSock[i], sendbuffer, strlen(sendbuffer), 0);
21 }
22 }
23
24 FILE * wfile = fopen("./allmsg.txt","a+");
25 fwrite(sendbuffer,strlen(sendbuffer),1,wfile);
26 fwrite("\n",strlen("\n"),1,wfile);
27 fclose(wfile);
28 printf("消息记录完毕\n");
29 printf("===================\n");
30 }
31 void *userthread(void *arg){
32
33 //char sendmsg[300];
34 int usersock = *(int *)arg;
35 printf("客户 Socket[ %d ]:\n", usersock);
36 while (1)
37 {
38 char usermsg[286] = {};
39 //memset(sendmsg, 0, sizeof(sendmsg));
40 memset(usermsg, 0, sizeof(usermsg));
41 if (recv(usersock, usermsg, sizeof(usermsg), 0) <= 0)
42 {
43 int i;
44 for (i = 0; i < MAXNUM; ++i)
45 {
46 if (usersock == clientSock[i])
47 {
48 clientSock[i] = 0;
49 break;
50 }
51 }
52
53 printf("用户 %d 已退出!\n", usersock);
54 pthread_exit((void *)i);
55 }
56
57 if(strcmp(usermsg,"[online]")==0)
58 {
59 printf("%d客户端查询消息记录\n",usersock);
60 FILE * sendfile = fopen("./allmsg.txt","r");
61 char allmsg[1024*10]={};
62 char allmsgsend[1024*11] = {};
63 fread(allmsg,sizeof(allmsg),1,sendfile);
64 strcat(allmsgsend,"\n--------漫游记录--------\n");
65 strcat(allmsgsend,allmsg);
66 strcat(allmsgsend,"\n-------漫游记录查询完毕---\n");
67 send(usersock,allmsgsend,strlen(allmsgsend),0);
68 fclose(sendfile);
69 printf("==================\n");
70 continue;
71 }
72 SendMsg(usermsg);
73 }
74}
75
76 int main(){
77 severSock = socket(AF_INET, SOCK_STREAM, 0);
78 struct sockaddr_in severAddr;
79 memset(&severAddr, 0, sizeof(severAddr));
80 severAddr.sin_family = AF_INET;
81 severAddr.sin_addr.s_addr = inet_addr(IP);
82 severAddr.sin_port = htons(PORT);
83
84 if (bind(severSock, (struct sockaddr *)&severAddr, sizeof(severAddr)) == -1)
85 {
86 perror("绑定失败!!!");
87 exit(-1);
88 }
89 if (listen(severSock, MAXNUM) == -1)
90 {
91 perror("监听失败!!!");
92 exit(-1);
93 }
94 printf("|****服务器启动成功****|\n");
95 // struct sockaddr_in tempaddr;
96 // socklen_t len = sizeof(tempaddr);
97
98 while (1)
99 {
100 struct sockaddr_in tempaddr;
101 socklen_t len = sizeof(tempaddr);
102 //memset(&tempaddr, 0, sizeof(tempaddr));
103 int sockid = accept(severSock, (struct sockaddr *)&tempaddr, &len);
104 if (sockid == -1)
105 {
106 printf("客户端连接失败!!!\n");
107 continue;
108 }
109 int i = 0;
110 for (i = 0; i < MAXNUM; ++i)
111 {
112 if (clientSock[i] == 0)
113 {
114 clientSock[i] = sockid;
115 printf("新的客户端已连接,socket: %d\n", sockid);
116 pthread_t userthrd;
117 pthread_create(&userthrd, 0, userthread, &sockid);
118 break;
119 }
120
121 if (i == MAXNUM)
122 {
123 char *str = "不好意思,满员了。";
124 send(sockid, str, strlen(str), 0);
125 close(sockid);
126 }
127 }
128 }
129}
今天你努力了吗
评论
0 评论