码迷,mamicode.com
首页 > 其他好文 > 详细

unix 局域网的TCP会话

时间:2015-11-20 12:19:16      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

client.c

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

#define maxn 101
/**
服务器端
1>建立一个基于TCP的流套接字描述符
2>将套接字描述符绑定到本地地址和本地端口上
3>监听客户端的请求
4>通过Accept接收客户端的请求
5>发送数据/接收数据
6>关闭套接字
客户端
1>与服务器端建立链接
2> 发送数据/接收数据
3>关闭套接字
**/
int main(int  argc, char** argv)
{
    int s_socket ;
    //1>建立一个基于TCP的流套接字描述符
    if((s_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        herror("socket");
        exit(errno);
    }
    else
    {
        printf("socket create success!\n");
    }
    //端口号
    unsigned int port;
    if(argv[2]) {
        port = atoi(argv[2]);
    }
    else {
        port = 1111;
    }
    //2>将套接字描述符绑定到本地地址和本地端口上
    struct sockaddr_in  addport;
    bzero(&addport,sizeof(addport));
    addport.sin_family = AF_INET;
    //addport.sin_addr.s_addr = inet_addr(argv[1]);
    addport.sin_port = htons(port);
    if(inet_aton(argv[1], (struct in_addr*)&addport.sin_addr.s_addr) == -1)
    {
        herror("port");
        exit(errno);
    }
    else
    {
        printf("IP and port  create success!\n");
    }
    //建立链接
    if(connect(s_socket, (struct sockaddr*)&addport, sizeof(addport)) == -1)
    {
        herror("connect");
        exit(errno);
    }
    else
    {
        printf("connect   success\n");
    }
    //3>监听客户端的请求
    char buf[maxn];
    while(1)
    {
        //接收信息
        bzero(buf, maxn);
        int len;
        len = recv(s_socket, buf, maxn, 0);
        if(len > 0)
        {
            printf("服务器发来的信息是:  %s\n", buf);
        }
        else
        {
            if(len == 0)
            {
                printf("socket is closed!");
            }
            else
            {
                printf("error!");
            }
        }
        //发送信息
    loop:
        bzero(buf, maxn);
        printf("请输入要发送的信息:");
        fgets(buf, maxn, stdin);
        if(strncasecmp(buf, "exit",4) == 0)
        {
            printf("聊天中止\n");
            break;
        }
        if(strncmp(buf,"\n",1) == 0)
        {
            printf("空字符\n");
            goto loop;
        }
        int L_len = strlen(buf);
        if(buf[L_len - 1] == \n)
        {
            len = send(s_socket, buf, L_len - 1, 0);
        }
        else
        {
            len = send(s_socket, buf, L_len, 0);
        }
        if(len > 0)
        {
            printf("send message success!\n");
        }
        else
        {
            printf("send message failure!\n");
            break;
        }
    }
    close(s_socket);
    return 0;
}

server.c

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

#define maxn 101
/**
服务器端
1>建立一个基于TCP的流套接字描述符
2>将套接字描述符绑定到本地地址和本地端口上
3>监听客户端的请求
4>通过Accept接收客户端的请求
5>发送数据/接收数据
6>关闭套接字
客户端
1>与服务器端建立链接
2> 发送数据/接收数据
3>关闭套接字
**/
int main(int  argc, char** argv)
{
    int s_socket, sock_port ;
    //1>建立一个基于TCP的流套接字描述符
    if((s_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        herror("socket");
        exit(errno);
    }
    else
    {
        printf("socket create success!\n");
    }
    //端口号
    unsigned int port;
    if(argv[2]) {
        port = atoi(argv[2]);
    }
    else {
        port = 1111;
    }
    if(argv[3])
    {
        sock_port = atoi(argv[3]);
    }
    else
    {
        sock_port = 3;
    }
    //2>将套接字描述符绑定到本地地址和本地端口上
    struct sockaddr_in  c_addport, s_addport;
    bzero(&s_addport, sizeof(s_addport));
    s_addport.sin_family = AF_INET;
    //s_addport.sin_addr.s_addr = inet_addr(argv[1]);
    s_addport.sin_port = htons(port);
    if(argv[1])
    {
        s_addport.sin_addr.s_addr = inet_addr(argv[1]);
    }
    else
    {
        s_addport.sin_addr.s_addr = INADDR_ANY;
    }
    if((bind(s_socket, (struct sockaddr*) &s_addport, sizeof(struct sockaddr))) == -1) {
        perror("bind");
        exit(errno);
    }
    else {
        printf("bind success!\n");
    }
    //3>监听客户端的请求
    if(listen(s_socket, sock_port) == -1)
    {
        herror("listen");
        exit(errno);
    }
    else
    {
        printf("server is listening!\n");
    }
    while(1)
    {
        int  flag;
        socklen_t len;
        len = sizeof(struct sockaddr);
        flag = accept(s_socket, (struct sockaddr*)&c_addport, &len);
        if(flag == -1)
        {
            herror("accept");
            exit(errno);
        }
        else
        {
            printf("Accept success!\n");
            printf("现在监听的客户端IP是 :%s端口是%d\n", inet_ntoa(c_addport.sin_addr), ntohl(c_addport.sin_port));
        }
        char buf[maxn];
        while(1)
        {
            //发送消息
        loop:
            bzero(buf, maxn);
            printf("请输入要发送的信息:");
            fgets(buf, maxn, stdin);
            if(strncasecmp(buf, "exit",4) == 0)
            {
                printf("chatting ending !\n");
                break;
            }
            if(strcmp(buf, "\n") == 0)
            {
                printf("空字符\n");
                goto loop;
            }
            int L_len = strlen(buf);
            int Index ;
            if(buf[L_len - 1] == \n)
            {
                Index = send(flag, buf, L_len - 1, 0);
            }
            else
            {
                Index = send(flag, buf, L_len, 0);
            }
            if(Index == -1)
            {
                printf("send message failure!");
            }
            else
            {
                printf("send message success!\n");
            }
            //接收消息
            bzero(buf, maxn);
            int LL;
            LL = recv(flag, buf, maxn, 0);
            if(LL > 0)
            {
                printf("从客户端接收的信息是%s\n", buf);
            }
            else
            {
                if(LL < 0)
                {
                    printf("Receive message failure!\n");
                }
                else
                {
                    printf("客户端退出,聊天中止\n");
                }
                break;
            }
        }
        close(flag);
        char ch[10];
        int  isok = 1;
        printf("是否结束进程?Y->是:N->否");
        bzero(ch, 10);
        fgets(ch, 10, stdin);
        if(strncasecmp(buf,"Y",1)== 0)
        {
            printf("结束进程");
            isok = 0;
            break;
        }
        if(!isok) break;
    }
    close(s_socket);
    return 0;
}

 

unix 局域网的TCP会话

标签:

原文地址:http://www.cnblogs.com/chenyang920/p/4980141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!