C語(yǔ)言編寫(xiě)基于TCP和UDP協(xié)議的Socket通信程序示例
Tcp多線(xiàn)程服務(wù)器和客戶(hù)端程序
服務(wù)器程序:
#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 步 創(chuàng)建套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
//第 2 步 設(shè)置地址結(jié)構(gòu)體
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 協(xié)議
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 步 監(jiān)聽(tīng)
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
客戶(hù)端程序 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 步 創(chuàng)建一個(gè)體套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
//第 2 步 設(shè)置 addr 結(jié)構(gòu)體
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 協(xié)議
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
//第 3 步 連接服務(wù)器
connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
write(sockfd,buf,strlen(buf)); //第 4 步 向套接字中寫(xiě)入字符串
}
}
Udp的服務(wù)器程序和客戶(hù)端程序
服務(wù)器程序:
#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 步 創(chuàng)建套接字
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
//第 2 步 設(shè)置地址結(jié)構(gòu)體
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 協(xié)議
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);//打印
}
}
客戶(hù)端程序 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 步 創(chuàng)建一個(gè)體套接字
int sockfd=socket(AF_INET,SOCK_DGRAM,0);
//第 2 步 設(shè)置 addr 結(jié)構(gòu)體
struct sockaddr_in svraddr;
svraddr.sin_family=AF_INET;//使用 internet 協(xié)議
svraddr.sin_port=htons(PORT);
inet_aton("127.0.0.1",&svraddr.sin_addr);
//第 3 步 連接服務(wù)器
//connect(sockfd,(struct sockaddr*)&svraddr,sizeof(svraddr));
while(1)
{
scanf("%s",buf);
sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&svraddr,sizeof(svraddr)); //第 4 步 向套接字中寫(xiě)入字符串
}
}
上一篇:編寫(xiě)C++程序使DirectShow進(jìn)行視頻捕捉
欄 目:C語(yǔ)言
下一篇:C++中實(shí)現(xiàn)隊(duì)列類(lèi)鏈?zhǔn)酱鎯?chǔ)與棧類(lèi)鏈?zhǔn)酱鎯?chǔ)的代碼示例
本文標(biāo)題:C語(yǔ)言編寫(xiě)基于TCP和UDP協(xié)議的Socket通信程序示例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2432.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文


