手机
当前位置:查字典教程网 >编程开发 >C语言 >C语言编写基于TCP和UDP协议的Socket通信程序示例
C语言编写基于TCP和UDP协议的Socket通信程序示例
摘要:Tcp多线程服务器和客户端程序服务器程序:#include#include#include#include#include#include#...

Tcp多线程服务器和客户端程序

服务器程序:

#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!rn"); int new_fd=*((int*)x); while(1) { int z=read(new_fd,buf,BUFSIZE);//第 6 步 读取套接字 if(z==0){printf("client close !");break;}; buf[z]=''; printf("%srn",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!rn");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])); } }

注意:

gcc server.c -o server -lpthread

客户端程序 cli.c

#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的服务器程序和客户端程序

服务器程序:

#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!rn");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]=''; printf("%srn",buf);//打印 } }

客户端程序 cli.c

#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 步 向套接字中写入字符串 } }

【C语言编写基于TCP和UDP协议的Socket通信程序示例】相关文章:

C 语言基础教程(我的C之旅开始了)[五]

关于C语言除0引发的思考

C 语言基础教程(我的C之旅开始了)[二]

C语言可变参数函数详解示例

C语言数组指针的小例子

C/C++语言中结构体的内存分配小例子

基于C++字符串替换函数的使用详解

C 语言基础教程(我的C之旅开始了)[三]

C语言小程序 如何判断三角型类型

C语言小程序 杨辉三角示例代码

精品推荐
分类导航