码迷,mamicode.com
首页 > 编程语言 > 详细

tcp-client-c++

时间:2015-01-09 06:57:51      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

#include "stdafx.h"
#include <Winsock2.h>
#include <iostream>
#pragma  comment(lib,"ws2_32.lib")
using namespace std;

void main()
{
    int err;
    WORD versionRequired;
    WSADATA wsaData;
    versionRequired=MAKEWORD(1,1);
    err=WSAStartup(versionRequired,&wsaData);//协议库的版本信息
    if (!err)
    {
        printf("客户端嵌套字已经打开!\n");
    }
    else
    {
        printf("客户端的嵌套字打开失败!\n");
        return;//结束
    }
    SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0);
    SOCKADDR_IN clientsock_in;
    clientsock_in.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
    clientsock_in.sin_family=AF_INET;
    clientsock_in.sin_port=htons(8001);
    //bind(clientSocket,(SOCKADDR*)&clientsock_in,strlen(SOCKADDR));//注意第三个参数
    //listen(clientSocket,5);
    connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//开始连接
    char receiveBuf[100];
    recv(clientSocket,receiveBuf,101,0);
    printf("%s\n",receiveBuf);
    //send(clientSocket,"hello,this is client",strlen("hello,this is client")+1,0);
    send(clientSocket,"123",strlen("123")+1,0);
    char receiveBuff[100];
    recv(clientSocket,receiveBuff,101,0);
    printf("%s\n",receiveBuff);
    closesocket(clientSocket);
    WSACleanup();
}

 

tcp-client-c++

标签:

原文地址:http://www.cnblogs.com/adjk/p/4212359.html

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