TCP相关代码

TCP 基础代码

//tcp_server.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>


int Startup(char* ip, int port)
{
    printf("port: %d\n", port);
    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = inet_addr(ip);
    //设置套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1)
    {
        perror("socket\n");
        exit(2);
    }
    //绑定
    if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0)
    {
        perror("bind\n");
        exit(3);
    }
    //监听
    int ret = listen(sock, 5);
    if(ret == -1)
    {
        perror("listen_sock\n");
        exit(3);
    }
    return sock;
}

void Server(int new_sock, char* ip, int port)
{
    char buf[1024];
    while(1)
    {
        buf[0] = 0;
        int s = read(new_sock, buf, sizeof(buf));
        if(s > 0)
        {
            buf[s] = 0;
            printf("[%s, %d] say# %s\n", ip, port, buf);
            write(new_sock, buf, strlen(buf));
        }
        else if(s == 0)
        {
            printf("[%s, %d] quit\n", ip, port);
            exit(4);
        }
        else
        {
            perror("read\n");
            exit(5);
        }
    }
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("Usage: %s [ip] [port]\n", argv[0]);
        exit(1);
    }

    //此时的套接字已经是监听套接字
    int listen_sock = Startup(argv[1], atoi(argv[2]));
    printf("listen and bind is sucessful!\n");

    //建立连接
    struct sockaddr_in peer;
    while(1)
    {
        socklen_t len = sizeof(peer);
        //此时的套接字已经是监听套接字
        int new_sock = accept(listen_sock, (struct sockaddr*)&peer, &len);
        //此时返回的这个套接字就是负责服务的套接字
        if(new_sock == -1)
        {
            printf("accept is error!\n");
            continue;
        }
        char* ip = inet_ntoa(peer.sin_addr);
        int port = ntohs(peer.sin_port);
        printf("get a new connet ip is %s, port is %d...\n", ip, port);
        //服务
        Server(new_sock, ip, port);
        //关闭文件描述符
    }
    return 0;
}
//tcp_client.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("Usage %s [ip], [port]\n", argv[0]);
        exit(1);
    }
    //建立套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1)
    {
        perror("socket\n");
        exit(2);
    }
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_port = htons(atoi(argv[2]));

    //发起连接
    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
    {
        perror("connect\n");
        exit(3);
    }
    while(1)
    {
        printf("Please enter#");
        fflush(stdout);
        char buf[1024];
        buf[0] = 0;
        int s = read(0, buf, sizeof(buf));
        if(s > 0)
        {
            buf[s - 1] = 0;
            if(strcmp(buf, "quit") == 0)
            {
                printf("client quit!\n");
            }
            write(sock, buf, strlen(buf));
            s = read(sock, buf, sizeof(buf) - 1);
            buf[s] = 0;
            printf("Server Echo#%s\n", buf);
        }
    }
    close(sock);
   return 0;
}

这里写图片描述
这里写图片描述

TCP 多进程多线程版本

//server.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("Usage %s [ip], [port]\n", argv[0]);
        exit(1);
    }
    //建立套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1)
    {
        perror("socket\n");
        exit(2);
    }
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_port = htons(atoi(argv[2]));

    //发起连接
    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
    {
        perror("connect\n");
        exit(3);
    }
    while(1)
    {
        printf("Please enter#");
        fflush(stdout);
        char buf[1024];
        buf[0] = 0;
        int s = read(0, buf, sizeof(buf));
        if(s > 0)
        {
            buf[s - 1] = 0;
            if(strcmp(buf, "quit") == 0)
            {
                printf("client quit!\n");
            }
            write(sock, buf, strlen(buf));
            s = read(sock, buf, sizeof(buf) - 1);
            buf[s] = 0;
            printf("Server Echo#%s\n", buf);
        }
    }
    close(sock);
   return 0;
}
//client.c
#include<stdio.h>
#include<error.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <arpa/inet.h>
#include<stdlib.h>
#include<error.h>

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("Usage %s [ip], [port]\n", argv[0]);
        exit(1);
    }
    //建立套接字
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1)
    {
        perror("socket\n");
        exit(2);
    }
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_port = htons(atoi(argv[2]));

    //发起连接
    if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
    {
        perror("connect\n");
        exit(3);
    }
    while(1)
    {
        printf("Please enter#");
        fflush(stdout);
        char buf[1024];
        buf[0] = 0;
        int s = read(0, buf, sizeof(buf));
        if(s > 0)
        {
            buf[s - 1] = 0;
            if(strcmp(buf, "quit") == 0)
            {
                printf("client quit!\n");
            }
            write(sock, buf, strlen(buf));
            s = read(sock, buf, sizeof(buf) - 1);
            buf[s] = 0;
            printf("Server Echo#%s\n", buf);
        }
    }
    close(sock);
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41027326/article/details/80460473