欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言編寫基于TCP和UDP協(xié)議的Socket通信程序示例

 更新時間:2016年03月15日 14:52:25   作者:陳仲陽0  
這篇文章主要介紹了C語言編寫基于TCP和UDP協(xié)議的Socket通信程序示例,其中TCP的客戶端與服務(wù)器端采用多線程實現(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)建一個體套接字
  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)建一個體套接字
  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)文章

  • opencv3/C++實現(xiàn)霍夫圓/直線檢測

    opencv3/C++實現(xiàn)霍夫圓/直線檢測

    今天小編就為大家分享一篇opencv3/C++實現(xiàn)霍夫圓/直線檢測,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C語言詳細講解const的用法

    C語言詳細講解const的用法

    今天探討const,首先來說是將變量常量化。為什么要將變量常量化,原因有諸多好處有諸多。比如可以使數(shù)據(jù)更加安全不會被修改
    2022-05-05
  • 深入探究C++中的容器適配器與仿函數(shù)技術(shù)

    深入探究C++中的容器適配器與仿函數(shù)技術(shù)

    C++中的容器適配器和仿函數(shù)是實現(xiàn)數(shù)據(jù)結(jié)構(gòu)與算法的重要技術(shù),容器適配器可以將一個容器轉(zhuǎn)換為另一個形式,仿函數(shù)則可以自定義數(shù)據(jù)類型的比較、排序、計算等行為,提高程序的靈活性和可重用性
    2023-04-04
  • C語言樹與二叉樹基礎(chǔ)全刨析

    C語言樹與二叉樹基礎(chǔ)全刨析

    二叉樹可以簡單理解為對于一個節(jié)點來說,最多擁有一個上級節(jié)點,同時最多具備左右兩個下級節(jié)點的數(shù)據(jù)結(jié)構(gòu)。本文將詳細介紹一下C中二叉樹與樹的概念和結(jié)構(gòu),需要的可以參考一下
    2022-04-04
  • Vs2022環(huán)境下安裝低版本.net framework的實現(xiàn)步驟

    Vs2022環(huán)境下安裝低版本.net framework的實現(xiàn)步驟

    本文主要介紹了Vs2022環(huán)境下安裝低版本.net framework的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C++數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹的實現(xiàn)

    C++數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹的實現(xiàn)

    了解搜索二叉樹是為了STL中的map和set做鋪墊,我們所熟知的AVL樹和平衡搜索二叉樹也需要搜索二叉樹的基礎(chǔ)。本文將詳解如何利用C++實現(xiàn)搜索二叉樹,需要的可以參考一下
    2022-05-05
  • 使用OpenGL創(chuàng)建窗口的示例詳解

    使用OpenGL創(chuàng)建窗口的示例詳解

    OpenGL,也就是Open?Graphics?Library。其主要就是用于我們?nèi)ヤ秩?D、3D矢量圖形的一種跨語言、跨平臺的應(yīng)用程序編程接口,這篇文章主要介紹了使用OpenGL創(chuàng)建窗口,需要的朋友可以參考下
    2022-04-04
  • 如何實現(xiàn)循環(huán)隊列

    如何實現(xiàn)循環(huán)隊列

    本文主要介紹了C語言循環(huán)隊列的實現(xiàn),對于數(shù)據(jù)結(jié)構(gòu)與算法的研究有所幫助,需要的朋友可以參考下
    2015-07-07
  • C++中指針的詳解及其作用介紹

    C++中指針的詳解及其作用介紹

    這篇文章主要介紹了C++中指針的詳解及其作用介紹,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • CLion安裝、漢化、配置圖文詳解

    CLion安裝、漢化、配置圖文詳解

    這篇文章主要介紹了CLion安裝、漢化、激活、配置圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11

最新評論