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

Linux系統(tǒng)創(chuàng)建TCP連接流程介紹

 更新時(shí)間:2021年12月01日 09:06:09   作者:王光杰:)  
大家好,本篇文章主要講的是Linux系統(tǒng)創(chuàng)建TCP連接流程,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下哦,方便下次瀏覽

Linux創(chuàng)建TCP的步驟

TCP編程需要客戶端和服務(wù)器兩套編碼,其創(chuàng)建TCP的流程也是不完全一致的

服務(wù)端

使用socket函數(shù)創(chuàng)建一個(gè)套接字

使用setsockopt函數(shù)設(shè)置套接字的屬性

使用bind函數(shù)綁定IP地址、端口信息到套接字上使用listen函數(shù)監(jiān)聽(tīng)指定端口

使用accept函數(shù)接收客戶端的連接請(qǐng)求

使用send/recv和read/write函數(shù)進(jìn)行數(shù)據(jù)的收發(fā)

使用close函數(shù)關(guān)閉網(wǎng)絡(luò)連接和監(jiān)聽(tīng)

客戶端

使用socket函數(shù)創(chuàng)建套接字使用setsockopt函數(shù)設(shè)置套接字屬性

使用bind函數(shù)綁定IP地址和端口信息

設(shè)置需要連接的IP地址和端口使用connect函數(shù)請(qǐng)求建立連接

使用send/recv和read/write函數(shù)進(jìn)行數(shù)據(jù)的收發(fā)

使用close函數(shù)關(guān)閉網(wǎng)路連接

TCP建立流程

Linux 創(chuàng)建TCP連接流程_linux

示例代碼

服務(wù)器

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#define MAXSIZE 128

char news[MAXSIZE];
int res;            //用以接收函數(shù)返回值

void* pthread_chat(void * arg)      //創(chuàng)建線程用以接收數(shù)據(jù)
{
    int confd = *(int *)arg;
    while(1)
    {
        res  = recv(confd, news, sizeof(news), 0);
        if(res <= 0)
        {
        perror("recv");
        break;
        }
        printf("The news is: %s\n",news);
        memset(news,0,MAXSIZE);
        send(confd,"OK",2,0);
    }

    printf("One client over\n");
    close(confd);
}

char *Time()                    //獲取當(dāng)前時(shí)間
{
    time_t timer;
    struct tm *tblock;
    timer = time(NULL);
    tblock = localtime(&timer);
    return asctime(tblock);
}

void save(char *s)              //儲(chǔ)存日志文件
{
    int fd;
    fd = open("journal",O_RDWR|O_APPEND|O_CREAT);

    if(fd < 0)
        perror("open");
    else
    {
        char *buf = Time();
        strcat(buf,s);

        write(fd,buf,MAXSIZE);
        lseek(fd,0,SEEK_END);

        if(res < 0)
            perror("write");
    }
}

int main()
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in saddr, caddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6666);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    res = bind(sockfd,(struct sockaddr*)&saddr, sizeof(saddr));
    if(res < 0)
        perror("bind");

    listen(sockfd, 5);          //監(jiān)聽(tīng)端口

    while(1)
    {
        int len = sizeof(caddr);
        int confd = accept(sockfd,(struct sockaddr*)&caddr, &len);
        if(confd < 0)
        {
            perror("accept");
            continue;
        }else
        {
            save(inet_ntoa(caddr.sin_addr));
        }

        printf("Accept confdis:%d, ip=%s\n",confd,inet_ntoa(caddr.sin_addr));

        pthread_t tid;
        pthread_create(&tid, NULL, pthread_chat, &confd);
    }
}

客戶端

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>  
#define MAXSIZE 128

char news[MAXSIZE];
int res;                    //用來(lái)接收函數(shù)返回值
int main()
{
    printf("------Welcome join the chat room-----\n");
    printf("If you want to quit,please input --bye--\n");
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6666);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int confd = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
    if(confd < 0)
        perror("connect");

    while(1)
    {
        printf("Please input the news\n");
        fgets(news,MAXSIZE,stdin);

        if(strncmp(news,"bye",3) == 0)
        {
            break;
        }

        send(sockfd, news, strlen(news), 0);
        memset(news,0,MAXSIZE);
        recv(sockfd, news, sizeof(news), 0);
          printf("The serve's news is: %s\n",news);
    }

    close(sockfd);
    exit(0);
}

請(qǐng)注意,服務(wù)端由于使用了多線程開(kāi)發(fā),需要在編譯時(shí)添加-lpthread選項(xiàng)

程序運(yùn)行效果如下:

Linux 創(chuàng)建TCP連接流程_網(wǎng)絡(luò)通信_(tái)02

到此這篇關(guān)于Linux系統(tǒng)創(chuàng)建TCP連接流程介紹的文章就介紹到這了,更多相關(guān)Linux創(chuàng)建TCP連接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Ubuntu 遠(yuǎn)程登陸服務(wù)器 ssh的安裝和配置詳解

    Ubuntu 遠(yuǎn)程登陸服務(wù)器 ssh的安裝和配置詳解

    這篇文章主要介紹了Ubuntu 遠(yuǎn)程登陸服務(wù)器 ssh的安裝和配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Ubuntu16.04 中 locate文件查找命令

    Ubuntu16.04 中 locate文件查找命令

    這篇文章主要介紹了Ubuntu16.04 中 locate文件查找命令,需要的朋友可以參考下
    2018-06-06
  • 深入理解Bash中的尖括號(hào)(適合初學(xué)者)

    深入理解Bash中的尖括號(hào)(適合初學(xué)者)

    這篇文章主要給大家介紹了關(guān)于Bash中尖括號(hào)的相關(guān)資料,本文非常適合初學(xué)者,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • linux_HDFS文件上傳后的追加報(bào)錯(cuò)問(wèn)題

    linux_HDFS文件上傳后的追加報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了linux_HDFS文件上傳后的追加報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Ubuntu掛載3T硬盤(pán)或大于2T磁盤(pán)的方法

    Ubuntu掛載3T硬盤(pán)或大于2T磁盤(pán)的方法

    本篇文章主要介紹了Ubuntu掛載3T硬盤(pán)或大于2T磁盤(pán)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Linux下利用unzip命令如何解壓多個(gè)文件詳解

    Linux下利用unzip命令如何解壓多個(gè)文件詳解

    這篇文章主要給大家介紹了關(guān)于在Linux下利用unzip命令如何解壓多個(gè)文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • 最新評(píng)論