Linux C/C++网络编程基础 TCP简单实现

简单的linux下socket编程,分别基于TCP协议实现的简单程序

linux下socket编程可以概括为以下几个函数的运用:

  • socket()
  • bind()
  • listen()
  • connect()
  • accept()
  • read()
  • write()
  • close()函数

Linux C/C++网络编程基础 TCP简单实现_第1张图片


server端

/*
*   socket简单编程 服务端
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    int server_sockfd = 0;
    int client_sockfd = 0;
    int len = 0;
    int sin_size = 0;
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    char buf[BUFFSIZE] = {0};

    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(8000);

    if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket error!\n");
        return -1;
    }

    if(bind(server_sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("bind error!\n");
        return -1;
    }

    if(listen(server_sockfd, 5) < 0)
    {
        perror("listen error!\n");
        return -1;
    }

    sin_size= sizeof(struct sockaddr_in);

    if((client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &sin_size)) < 0)
    {
        perror("accept error!\n");
        return -1;
    }

    printf("accept client %s\n",inet_ntoa(client_addr.sin_addr));
    len = send(client_sockfd, "Hello word!\n",30,0);

    while((len = recv(client_sockfd, buf, BUFFSIZE, 0)) >0)
    {
        buf[len] = '\0';
        printf("buf = %s\n",buf);
        if(send(client_sockfd, buf, len, 0) < 0)
        {
            perror("send error!\n");
            return -1;
        }
    }

    close(client_sockfd);
    close(server_sockfd);

    return 0;
}

client端

/*
*   socket简单编程 客户端
 */

#include 
#include 
#include 
#include 
#include 
#include 

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    int client_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    char buf[BUFFSIZE] = {0};
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(8000);

    if((client_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket error!\n");
        return -1;
    }

    if(connect(client_sockfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        perror("client error!\n");
        return -1;
    }

    printf("connect success!\n");

    len = recv(client_sockfd, buf, BUFFSIZE, 0);
    buf[len] = '\0';
    printf("client_buf = %s\n",buf);

    while(1)
    {
        printf("Enter string to send:");
        scanf("%s",buf);
        if(!strcmp(buf,"quit"))
        {
            break;
        }

        len = send(client_sockfd, buf, strlen(buf), 0);
        len = recv(client_sockfd, buf, BUFFSIZE, 0);
        buf[len] = '\0';
        printf("received: %s\n",buf);
    }

    close(client_sockfd);

    return 0;
}

你可能感兴趣的:(Linux,C网络编程)