简单unix 局域网的TCP会话

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];
    char  buf1[maxn];
    int flag = 1;
    while(1)
    {
        //接收信息
        flag = 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("server is closed!\n");
                flag = 0;
                break;
            }
            else if(len == -1)
            {
                printf("receive message failure!");
            }
        }
    loop:
        if(flag == 0) break;
        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");
            continue;
        }
        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;
}
View Code

 

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);
            int L_len = strlen(buf);
            int Index ;
            if(strncasecmp(buf, "exit", 4) == 0)
            {
                printf("chatting  be ended !\n");
                break;
            }
            if(strcmp(buf, "\n") == 0)
            {
                printf("空字符\n");
                goto loop;
            }
            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);
        printf("是否结束进程?Y->是:N->否");
        bzero(buf, maxn);
        fgets(buf, maxn, stdin);
        if(strncasecmp(buf, "Y", 1) == 0)
        {
            printf("结束进程\n");
            break;
        }
        else {
            printf("waiting ..........\n");
        }
    }
    close(s_socket);
    return 0;
}
View Code

 

应用少.....更新中。。。。。。

posted on 2015-11-20 11:17  `Elaine  阅读(235)  评论(0编辑  收藏  举报

导航