基于TCP的通信 客户端

#include <WINSOCK2.H>  

#include <stdio.h>
// socket 套接字 #pragma comment (lib,"Ws2_32") //library DWORD WINAPI ThreadInput(LPVOID lpParam);
DWORD WINAPI ThreadOutput(LPVOID lpParam); SOCKET sClient; void main()
{
//first 初始化DLL
WSADATA wsadata;
if (WSAStartup(MAKEWORD(,),&wsadata)!=)
{
printf("Init Error\n"); return ;
} //second 创建套接字 sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (sClient == INVALID_SOCKET)
{
printf("Create Listen Is Error\n"); return;
} sockaddr_in LocalAddr; LocalAddr.sin_family = AF_INET; LocalAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //127.0.0.1 LocalAddr.sin_port = htons(); if (connect(sClient,(sockaddr*)&LocalAddr,sizeof(LocalAddr))==SOCKET_ERROR)
{ // WSAENETDOWN
int a = GetLastError();
closesocket(sClient); printf("Connect Is Error\n"); return;
} HANDLE hThread[]; //这里是整个程序的核心部分我们一定要那个管道图 hThread[] = CreateThread(NULL,,ThreadOutput,NULL,,NULL); hThread[] = CreateThread(NULL,,ThreadInput,NULL,,NULL); WaitForMultipleObjects(,hThread,TRUE,INFINITE); } DWORD WINAPI ThreadInput(LPVOID lpParam)
{
SOCKET s = sClient; //该线程函数是从套接字中获取信息并且通过管道注入cmd的工作函数 char Buffer[] = {}; DWORD dWrite = ;
DWORD dwRecvd = ;
while (TRUE)
{ scanf("%s",Buffer); send(s,Buffer,strlen(Buffer),);
} return ;
} DWORD WINAPI ThreadOutput(LPVOID lpParam)
{ SOCKET s = sClient; //该线程函数是从被控端返给控端信息的线程函数 char Buffer[]={};
while (TRUE)
{ int Ret = recv(s,Buffer,,); if(Ret<)
{
break;
} Buffer[Ret]='\0'; printf("%s",Buffer); } return ;
}
上一篇:CSS优化技巧


下一篇:python开发_configparser_解析.ini配置文件工具_完整版_博主推荐