tcp的一个简单例子

1.客户端简单例子

client.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#ifdef _WIN32
#include <winsock2.h>
#else
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#endif

#define MAXLINE 1024
#define SERVERPORT 9999

using namespace std;

int main(int argc,char **argv)
{
    if( argc != 2 )
        error_quit("Using: ./client <IP Address>");

    int sockfd,n;
    char receline[MAXLINE+1];
    struct sockaddr_in serveraddr;

    #ifdef _WIN32
    //Winsows下启用socket
    WSADATA wsadata;
    if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
    {
        printf("WSAStartup() fail\n");
        exit(0);
    }
    #endif

    //建立socket
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        printf("socket() fail\n");
        exit(0);
    }

    //设置协议及Port
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port=htons(SERVERPORT);

    //设置IP
    string ip(argv[1]);
    serveraddr.sin_addr.s_addr=inet_addr(ip.c_str());

    //连接
    if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))==-1)
    {
        printf("connect() fail\n");
        exit(0);
    }

    //读取数据并输入到标准输出
    #ifdef _WIN32
    while((n=recv(sockfd,receline,MAXLINE,0))>0)
    #else
    while((n=read(sockfd,receline,MAXLINE))>0)
    #endif
    {
        receline[n]=0;
        if(fputs(receline,stdout)==EOF)
        {
            printf("fputs() error\r\n");
        }
    }

    //没有获取数据
    if(n<0)
    {
        printf("read() fail\n");
    }

    #ifdef _WIN32
    //Winsows下关闭socket
    closesocket(sockfd);
    WSACleanup();
    #endif

    return 0;
}

2.服务端简单例子

server.cpp

#define MINGW32

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> 

#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#endif

#define LISTENQ 10
#define SERVERPORT 9999

int main(int argc,char *argv[])
{
    int serverfd,connectfd;
    struct sockaddr_in serveraddr;
    char buff[1024];
    time_t tlick;
    int iRet;

    #ifdef _WIN32
    //Winsows下启用socket
    WSADATA wsadata;
    if(WSAStartup(MAKEWORD(1,1),&wsadata)==SOCKET_ERROR)
    {
        printf("WSAStartup() fail\n");
        exit(0);
    }
    #endif

    //新建socket
    printf("socket()\n");
    serverfd=socket(AF_INET,SOCK_STREAM,0);
    if(serverfd==-1)
    {
        printf("socket() fail\n");
        exit(0);
    }

    //清零
    memset(&serveraddr,0,sizeof(serveraddr));

    //设置协议
    serveraddr.sin_family=AF_INET;
    //设置IP
    serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
    //设置Port
    serveraddr.sin_port=htons(SERVERPORT);

    //绑定端口,监听1024端口的任何请求
    printf("bind()\n");
    iRet=bind(serverfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
    if(iRet==-1)
    {
        printf("bind() fail\n");
        exit(0);
    }

    //监听端口,最大并发数10
    printf("listen()\n");
    iRet=listen(serverfd,LISTENQ);
    if(iRet==-1)
    {
        printf("listen() fail\n");
        exit(0);
    }

    //接受请求,发送主机时间
    for(;;)
    {
        printf("Waiting for connection...\n");
        //接受请求
        connectfd=accept(serverfd,(struct sockaddr*)NULL,NULL);
        //获取时间
        tlick=time(NULL);
        //格式化时间
        snprintf(buff,sizeof(buff),"From mys:\n%s",ctime(&tlick));
        //写入时间
        //关闭请求
        #ifdef _WIN32
        send(connectfd,buff,strlen(buff),0);
        closesocket(connectfd);
        #else
        write(connectfd,buff,strlen(buff));
        close(connectfd);
        #endif
    }

    #ifdef _WIN32
    //Winsows下关闭socket
    closesocket(serverfd);
    WSACleanup();
    #endif

    //退出
    exit(0);
}


3.Makefile

all:server client

server:server.cpp
    gcc -g -o server server.cpp -l wsock32 -l stdc++

client:client.cpp
    gcc -g -o client client.cpp -l wsock32 -l stdc++

clean:
    del *~ *.o *.exe
文章目录