C語(yǔ)言編寫基于TCP和UDP協(xié)議的Socket通信程序示例
更新時(shí)間:2016年03月15日 14:52:25 作者:陳仲陽(yáng)0
這篇文章主要介紹了C語(yǔ)言編寫基于TCP和UDP協(xié)議的Socket通信程序示例,其中TCP的客戶端與服務(wù)器端采用多線程實(shí)現(xiàn),需要的朋友可以參考下
Tcp多線程服務(wù)器和客戶端程序
服務(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)聽
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 步 創(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 步 向套接字中寫入字符串
}
}
Udp的服務(wù)器程序和客戶端程序
服務(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);//打印
}
}
客戶端程序 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 步 向套接字中寫入字符串
}
}
相關(guān)文章
C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交
這篇文章主要介紹了C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個(gè)鏈表是否相交的方法,文中還給出了求兩個(gè)鏈表相交的第一個(gè)節(jié)點(diǎn)列的實(shí)現(xiàn)方法,需要的朋友可以參考下2016-02-02
Visual Studio 2022最新版安裝教程(圖文詳解)
本文主要介紹了Visual Studio 2022最新版安裝教程,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C語(yǔ)言技巧提升之回調(diào)函數(shù)的掌握
這篇文章主要為大家詳細(xì)介紹一下C語(yǔ)言中回調(diào)函數(shù)的用法教程,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定幫助,需要的可以參考一下2022-12-12
C++ 輕量級(jí)對(duì)象JSON序列化實(shí)現(xiàn)詳情
本文以jsoncpp庫(kù)為基礎(chǔ),設(shè)計(jì)這樣一個(gè)可以支持一個(gè)函數(shù) 可以一行代碼 unmarshal /marshal 對(duì)象,需要的朋友小伙伴可以參考以下2021-09-09
C++線程優(yōu)先級(jí)SetThreadPriority的使用實(shí)例
這篇文章主要介紹了C++線程優(yōu)先級(jí)SetThreadPriority的使用實(shí)例,較為詳細(xì)的講述了C++線程及其優(yōu)先級(jí)的用法,需要的朋友可以參考下2014-10-10
C++之a(chǎn)ssert推薦用法及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于C++之a(chǎn)ssert推薦用法及注意事項(xiàng)的相關(guān)資料,assert 是一個(gè)預(yù)處理宏,用于在運(yùn)行時(shí)檢查表達(dá)式是否為真,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07

