Socket(发送,接收)

send.cpp:

#include 
#include 
#include 

#pragma comment(lib,"ws2_32.lib")

int main()
{
    unsigned long ip;
    ip=inet_addr("127.0.0.1");

    short port;
    port=8000;

    printf("Connecting to 127.0.0.1:8000....");

    WSADATA wsa;

    if (WSAStartup(MAKEWORD(2,2),&wsa)!=0)
    {
        printf("socket init error");
        exit(-1);
    }

    SOCKET sock;

    if ((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_ATOM)
    {
        printf("create socket error");
        exit(-1);
    }

    struct sockaddr_in serverAddress;
    memset(&serverAddress,0,sizeof(sockaddr_in));
    serverAddress.sin_family=AF_INET;
    serverAddress.sin_addr.S_un.S_addr=ip;
    serverAddress.sin_port=htons(port);

    if (connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR)
    {
        printf("link error");
        exit(-1);
    }

    char buf[4096];
    while (1)
    {
        printf(">");
        gets(buf);

        if (send(sock,buf,strlen(buf),0)==SOCKET_ERROR)
        {
            printf("send error");
            exit(-1);
        }
        int bytes;

        if ((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR)
        {
            printf("receive error");
            exit(-1);
        }

        buf[bytes]='\0';
        printf("Message from %s:%s\n",inet_ntoa(serverAddress.sin_addr),buf);

    }

    WSACleanup();
    return 0;

}

receive.cpp:

#include 
#include 
#include 

#pragma comment(lib,"ws2_32.lib")

int main(int argc,char *argv[])
{

    short port;

    port=8000;
    WSADATA wsa;

    if (WSAStartup(MAKEWORD(2,2),&wsa)!=0)
    {
        printf("socket init error");
        exit(-1);
    }

    SOCKET serverSocket;

    if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
    {
        printf("创建套接字失败!");
        exit(-1);
    }

    struct sockaddr_in serverAddress;
    memset(&serverAddress,0,sizeof(sockaddr_in));
    serverAddress.sin_family=AF_INET;
    serverAddress.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
    serverAddress.sin_port=htons(port);

    if (bind(serverSocket,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR)
    {
        printf("socket port error:%d",port);
        exit(-1);
    }


    if (listen(serverSocket,SOMAXCONN)==SOCKET_ERROR)
    {
        printf("listen error");
        exit(-1);
    }
    printf("Server 8000 is listening......\n");
    SOCKET clientSocket;
    struct sockaddr_in clientAddress;

    memset(&clientAddress,0,sizeof(clientAddress));
    int addrlen=sizeof(clientAddress);

    if ((clientSocket=accept(serverSocket,(sockaddr*)&clientAddress,&addrlen))==INVALID_SOCKET)
    {
        printf("link error");
        exit(-1);
    }

    char buf[4096];

    while (1)
    {
        int bytes;
        if ((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR)
        {
            printf("data error");
            exit(-1);
        }

        buf[bytes]='\0';
        printf("Message from %s: %s\n",inet_ntoa(clientAddress.sin_addr),buf);
        if(send(clientSocket,buf,bytes,0)==SOCKET_ERROR)
        {
            printf("发送数据失败!");
            exit(-1);
        }

    }

    WSACleanup();
    exit(-1);

}

别人的程序,工程中需要用到socket

转载于:https://www.cnblogs.com/tiandsp/archive/2012/04/20/2459721.html

你可能感兴趣的:(Socket(发送,接收))