linux网络编程(socket)之面向连接(TCP/IP)

1、流程

服务器:

  创建socket;

  绑定端口;

  监听;

  监听到有连接请求,接受请求;

  建立连接,开始对话。

客户端:

  创建socket;

  请求建立连接;

  连接建立成功,开始对话。

2、实例代码

//服务器客户端简单聊天工具之服务器(tcp/ip)
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>

//连接端口
#define PORT 6666

//最大连接数
#define CONNECTION 10
int main() 
{
    int sockfd, client_fd,sendnum,recvnum;
    char inbuf[100],outbuf[100];
    socklen_t addr_size;
    pid_t pid;
    struct sockaddr_in my_addr, client_addr;

    //socket创建
    sockfd = socket(PF_INET,SOCK_STREAM,0);
    if(sockfd < 0) 
    {
        perror("socket fail! \n");
        return 1;
    }

    //服务器的地址
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(PORT);
    my_addr.sin_addr.s_addr = INADDR_ANY;

    //绑定端口
    if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr)) < 0) 
    {
        perror("bind fail! \n"); 
        close(sockfd);
        return 1;
    }
    
    //开始监听
    if(listen(sockfd,CONNECTION) < 0)
    {
        perror("listen fail! \n");
        close(sockfd);
        return 1;
    }
    addr_size = sizeof(struct sockaddr_in);

    //建立连接
    if((client_fd = accept(sockfd,(void *)&client_addr,&addr_size)) < 0)
    {
        perror("accept fail! \n");
        close(sockfd);
        return 1;
    }
    printf("receive connection! \n");

    //启动子进程
    pid = fork();
    if(pid < 0)
    {
        perror("fork fail! \n");
        close(client_fd);
        close(sockfd);
        return 1;
    }

    //子进程发送消息
    else if(pid == 0)
    {
        while(1)
        {
            fgets(inbuf,sizeof(inbuf),stdin);
            sendnum = send(client_fd,(void*)inbuf,sizeof(inbuf),0);
            if(sendnum < 0)
            {
                perror("send fail! \n");
                break;
            }
        }
    }

    //父进程接收消息
    else if(pid > 0)
    {
        while(1)
        {
            recvnum = recv(client_fd,(void*)outbuf,sizeof(outbuf),0);
            if(recvnum < 0)
            {
                perror("recv fail! \n");
                break;
            }
            if(recvnum == 0)
            {
                break;
            }
            printf("%s:%s",inet_ntoa(client_addr.sin_addr),outbuf);
        }
    }
    close(client_fd);
    close(sockfd);
    return 0;
}

 

//服务器客户端简单聊天工具之客户端(tcp/ip)
#include<stdio.h>
#include<errno.h>
#include<sys/socket.h>
#include<unistd.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<arpa/inet.h>
#define PORT 6666

//连接端口
#define IP "127.0.0.1"
int main()
{
    int sockfd,sendnum,recvnum;
    char inbuf[100],outbuf[100];
    pid_t pid;
    struct sockaddr_in serv_addr;
    
    //socket创建
    sockfd = socket(PF_INET,SOCK_STREAM,0);
    if(sockfd < 0) 
    {
        perror("socket fail! \n");
        return 1;
    }

    //服务器的地址
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    serv_addr.sin_addr.s_addr = inet_addr(IP);

    //连接服务器
    if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)) < 0)
    {
        perror("connect fail! \n");
        close(sockfd);
        return 1;
    }
    printf("connect sucessful! \n");

    //启动子进程
    pid = fork();
    if(pid < 0)
    {
        perror("fork fail! \n");
        close(sockfd);
        return 1;
    }

    //子进程发送消息
    else if(pid == 0)
    {
        while(1)
        {
            fgets(inbuf,sizeof(inbuf),stdin);
            sendnum = send(sockfd,(void*)inbuf,sizeof(inbuf),0);
            if(sendnum < 0)
            {
                perror("send fail! \n");
                break;
            }
        }
    }

    //父进程接收消息
    else if(pid > 0) 
    {
        while(1)
        {
            recvnum = recv(sockfd,(void*)outbuf,sizeof(outbuf),0);
            if(recvnum < 0)
            {
                perror("recv fail! \n");
                break;
            }
            if(recvnum == 0)
            {
                break;
            }
            printf("%s:%s",inet_ntoa(serv_addr.sin_addr),outbuf);
        }
    }    
    close(sockfd);
    return 0;
}
  

  

posted @ 2014-02-25 15:02  不吃早饭好不好  阅读(316)  评论(0编辑  收藏  举报