Tcp多线程服务器和客户端程序
服务器程序:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
void* fun(void* x)
{
//printf("enter thread!\\r\\n");
int new_fd=*((int*)x);
while(1)
{
int z=read(new_fd,buf,BUFSIZE);//第 6 步 读取套接字
if(z==0){printf("client close !");break;};
buf[z]='\\0';
printf("%s\\r\\n",buf);//打印
};
}
int newfd[512];
int inewfd=0;
int main()
{
//第 1 步 创建套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
//第 2 步 设置地址结构体
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 协议
svraddr.sin_port=htons(PORT);
inet_aton("0.0.0.0",&svraddr.sin_addr);
//第 3 步 绑定
int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
if(ret<0){printf("error bind!\\r\\n");exit(-1);};
//第 4 步 监听
listen(sockfd,128);
while(1)
{
newfd[inewfd++]=accept(sockfd,NULL,NULL); //第 5 步 接收
pthread_t ntid;
pthread_create(&ntid,NULL,fun,(void*)&(newfd[inewfd-1]));
}
}
|
注意:
?
|
1
|
gcc server.c -o server -lpthread
|
客户端程序 cli.c
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
//第 1 步 创建一个体套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
//第 2 步 设置 addr 结构体
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 协议
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
//第 3 步 连接服务器
connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
write(sockfd,buf,strlen(buf)); //第 4 步 向套接字中写入字符串
}
}
|
Udp的服务器程序和客户端程序
服务器程序:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
//第 1 步 创建套接字
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
//第 2 步 设置地址结构体
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 协议
svraddr.sin_port=htons(PORT);
inet_aton("0.0.0.0",&svraddr.sin_addr);
//第 3 步 绑定
int ret=bind(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
if(ret<0){printf("cannot bind!\\r\\n");exit(-1);};
while(1)
{
struct sockaddr_in cli;
int len=sizeof(cli);
int z=recvfrom(sockfd,buf,BUFSIZE,0,(struct sockaddr*)&cli,&len);//第 6 步 读取套接字
buf[z]='\\0';
printf("%s\\r\\n",buf);//打印
}
}
|
客户端程序 cli.c
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8082
#define BUFSIZE 512
char buf[BUFSIZE+1];
int main()
{
//第 1 步 创建一个体套接字
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
//第 2 步 设置 addr 结构体
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 协议
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
//第 3 步 连接服务器
//connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&svraddr,sizeof(svraddr)); //第 4 步 向套接字中写入字符串
}
}
|
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 49
-
2025-05-29 47
-
2025-05-27 58
-
2025-05-25 52
-
2025-06-04 55
热门评论

