亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

c語言網(wǎng)絡(luò)編程-標(biāo)準(zhǔn)步驟(比較簡單)

 更新時(shí)間:2014年01月30日 22:17:27   作者:  
這篇文章主要介紹了c語言網(wǎng)絡(luò)編程-標(biāo)準(zhǔn)步驟(比較簡單),需要的朋友可以參考下

c語言網(wǎng)絡(luò)編程-標(biāo)準(zhǔn)步驟,真的很簡單啊

server.c

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define PORT 4444
#define BACKLOG 5

int main(int argc, char *argv[]) {
    int sock_fd, new_fd;
    struct sockaddr_in server_addr, client_addr;
    int sin_size;
    int nbytes;
    int on = 1;
    char buffer[1024];

    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family      = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port        = htons(PORT);

    if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sock_fd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    printf("Server start... \n");

    while (1) {
        sin_size = sizeof(struct sockaddr_in);
        if ((new_fd = accept(sock_fd, (struct sockaddr *)(&client_addr), &sin_size)) == -1) {
            perror("accept");
            exit(1);
        }

        printf("Server get connection from %s\n", inet_ntoa(client_addr.sin_addr));
        if ((nbytes = read(new_fd, buffer, 1024)) == -1) {
            perror("read");
            exit(1);
        }
        buffer[nbytes] = '\0';
        printf("Server received: %s\n", buffer);
        close(new_fd);
    }

    return 0;
}

client.c

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 4444

int main(int argc, char *argv[]) {
    int sock_fd;
    struct sockaddr_in server_addr;
    struct hostent *host;
    char buffer[1024];

    if (argc < 2) {
        perror("Need hostname");
        exit(1);
    }

    if ((host = gethostbyname(argv[1])) == NULL) {
        perror("gethostbyname");
        exit(1);
    }

    if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(struct sockaddr_in));
    server_addr.sin_family  = AF_INET;
    server_addr.sin_port    = htons(PORT);
    server_addr.sin_addr    = *((struct in_addr *)host->h_addr);

    if (connect(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
        perror("connect");
        exit(1);
    }

    printf("Please input something:\n");
    fgets(buffer, 1024, stdin);
    write(sock_fd, buffer, strlen(buffer));
    close(sock_fd);

    return 0;
}

相關(guān)文章

  • C/C++獲取主機(jī)網(wǎng)卡MAC地址的三方法

    C/C++獲取主機(jī)網(wǎng)卡MAC地址的三方法

    MAC地址(Media Access Control address),又稱為物理地址或硬件地址,是網(wǎng)絡(luò)適配器(網(wǎng)卡)在制造時(shí)被分配的全球唯一的48位地址,通過獲取MAC地址可以判斷當(dāng)前主機(jī)的唯一性可以與IP地址綁定并實(shí)現(xiàn)網(wǎng)絡(luò)準(zhǔn)入控制,本文給大家介紹了使用C/C++獲取主機(jī)網(wǎng)卡MAC地址的三方法
    2023-11-11
  • C++中template方法undefined reference to的問題解決

    C++中template方法undefined reference to的問題解決

    Undefined reference to 錯(cuò)誤:這類錯(cuò)誤是在連接過程中出現(xiàn)的,本文就來介紹一下C++中template方法undefined reference to的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • 總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法

    總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法

    本文總結(jié)了下UNIX/LINUX下C++程序計(jì)時(shí)的一些函數(shù)和方法,對日常使用C++程序的朋友很有幫助,有需要的小伙伴們可以參考學(xué)習(xí),下面一起來看看吧。
    2016-08-08
  • C語言實(shí)現(xiàn)簡單的聊天室功能

    C語言實(shí)現(xiàn)簡單的聊天室功能

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Qt利用QNetwork實(shí)現(xiàn)上傳數(shù)據(jù)的示例代碼

    Qt利用QNetwork實(shí)現(xiàn)上傳數(shù)據(jù)的示例代碼

    這篇文章主要為大家詳細(xì)介紹了Qt如何利用QNetwork實(shí)現(xiàn)上傳數(shù)據(jù)的 功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-02-02
  • 詳解c++中的異常

    詳解c++中的異常

    程序在運(yùn)行過程中,有對也就有錯(cuò),正確那么就不用說了,但是如果錯(cuò)誤,那么我們?nèi)绾慰焖俚亩ㄎ坏藉e(cuò)誤的位置,以及知道發(fā)生了什么錯(cuò)誤。當(dāng)一個(gè)函數(shù)發(fā)現(xiàn)自己無法處理的異常,就會(huì)拋出一個(gè)異常,讓函數(shù)調(diào)用者直接或者間接的處理這個(gè)錯(cuò)誤。本文將詳解介紹c++中的異常
    2021-06-06
  • Visual Studio 2019修改編碼UTF-8的實(shí)現(xiàn)

    Visual Studio 2019修改編碼UTF-8的實(shí)現(xiàn)

    這篇文章主要介紹了Visual Studio 2019修改編碼UTF-8的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 詳解如何利用C++實(shí)現(xiàn)Mystring類

    詳解如何利用C++實(shí)現(xiàn)Mystring類

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)MyString的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 使用C++和代理IP實(shí)現(xiàn)天氣預(yù)報(bào)的采集

    使用C++和代理IP實(shí)現(xiàn)天氣預(yù)報(bào)的采集

    在當(dāng)今的互聯(lián)網(wǎng)時(shí)代,網(wǎng)絡(luò)信息的獲取變得日益重要,天氣預(yù)報(bào)信息作為日常生活的重要參考,其獲取方式也隨著技術(shù)的發(fā)展而不斷變化,在本文中,我們將探討如何使用C++和代理IP來采集天氣預(yù)報(bào)信息,文中通過代碼講解的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • C/C++關(guān)于實(shí)現(xiàn)CAN信號的獲取方法

    C/C++關(guān)于實(shí)現(xiàn)CAN信號的獲取方法

    這篇文章主要介紹了C/C++關(guān)于實(shí)現(xiàn)CAN信號的獲取方法,標(biāo)準(zhǔn)的CAN 數(shù)據(jù)為8字節(jié),即64位,但是CAN FD的最大數(shù)據(jù)可為64字節(jié),為512位,其中的幀ID分為標(biāo)準(zhǔn)幀和擴(kuò)展幀,其中用11位標(biāo)準(zhǔn)幀,用29位表示擴(kuò)展幀
    2023-02-02

最新評論