首页 > 代码库 > tcp-client-c++
tcp-client-c++
#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++
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。