客户端

// TcpSocket_Client.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#pragma comment(lib, "Ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
    WSADATA wsa;
    WORD version = MAKEWORD(1,1);
    int errInfo = WSAStartup(version,&wsa);
    if(errInfo != NOERROR)
    {
        printf_s("socket init error code = %d",errInfo);
        return 0;
    }
    int _socket = socket(AF_INET,SOCK_STREAM,0);
    sockaddr_in _sockaddr_in;
    _sockaddr_in.sin_family = AF_INET;
    _sockaddr_in.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    _sockaddr_in.sin_port = htons(8972);

    connect(_socket,(sockaddr*)&_sockaddr_in,sizeof(_sockaddr_in));

    char send_buf[1024] = {"这里是客户端\n"};

    send(_socket,send_buf,sizeof(send_buf),0);

    char recv_buf[1024];
    recv(_socket,recv_buf,sizeof(recv_buf),0);

    printf_s("data is %s",recv_buf);
    system("pause");
    return 0;
}





你可能感兴趣的:(客户端)