//serve
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <WINSOCK2.H>
#include <STDIO.H>
#include <stdio.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
BOOL WINAPI QueueUserWorkItem(LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags)
{
typedef BOOL (WINAPI *LPQueueUserWorkItem)(LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags);
LPQueueUserWorkItem pfnQueueUserWorkItem = NULL;
pfnQueueUserWorkItem = (LPQueueUserWorkItem)::GetProcAddress(::LoadLibrary("kernel32.dll"), "QueueUserWorkItem");
if ( NULL == pfnQueueUserWorkItem )
{
return FALSE;
}
else
{
return pfnQueueUserWorkItem(Function, Context, Flags);
}
}
DWORD WINAPI receive(LPVOID lpThreadParameter)
{
//初始化WSA
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
// unsigned long flag;
// flag=0;
if(WSAStartup(sockVersion, &wsaData)!=0)
{
return 0;
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(slisten == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
// if(ioctlsocket(slisten,FIONBIO, &flag)!=0)
// printf("fei zu se shibai \n");
//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8889);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("bind error !");
}
while(1)
{
if(listen(slisten, 5) == SOCKET_ERROR)
{
printf("listen error !");
return 0;
}
//循环接收数据
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
//开始监听
// printf("..............\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf("accept error !");
}
// printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
//接收数据
int ret = recv(sClient, revData, 255, 0);
if(ret > 0)
{
revData[ret] = 0x00;
printf("YOU :");
printf(revData);
printf("\n");
}
}
closesocket(slisten);
WSACleanup();
return 0;
}
DWORD WINAPI send(LPVOID lpThreadParameter)
{
char sdata[1000];
WORD sockVersion = MAKEWORD(2,2);
WSADATA data;
unsigned long flag;
flag=0;
if(WSAStartup(sockVersion, &data) != 0)
{
return 0;
}
while(1)
{
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sclient == INVALID_SOCKET)
{
printf("invalid socket !");
return 0;
}
//非阻塞
if(ioctlsocket(sclient,FIONBIO, &flag)!=0)
printf("fei zu se shibai \n");
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8888);
serAddr.sin_addr.S_un.S_addr = inet_addr("169.254.10.6");
if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("connect error !");
closesocket(sclient);
return 0;
}
memset(sdata,0,1000);
printf("I :");
scanf("%s",sdata);
send(sclient, sdata, strlen(sdata), 0);
closesocket(sclient);
}
WSACleanup();
return 0;
}
int main()
{
QueueUserWorkItem(receive, (void*)1 ,0x00000020);
QueueUserWorkItem(send, (void*)1 , 0x00000020);
Sleep(100000);
return 0;
}
//CLIENT
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <WINSOCK2.H>
#include <STDIO.H>
#include <stdio.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
BOOL WINAPI QueueUserWorkItem(LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags)
{
typedef BOOL (WINAPI *LPQueueUserWorkItem)(LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags);
LPQueueUserWorkItem pfnQueueUserWorkItem = NULL;
pfnQueueUserWorkItem = (LPQueueUserWorkItem)::GetProcAddress(::LoadLibrary("kernel32.dll"), "QueueUserWorkItem");
if ( NULL == pfnQueueUserWorkItem )
{
return FALSE;
}
else
{
return pfnQueueUserWorkItem(Function, Context, Flags);
}
}
DWORD WINAPI n(LPVOID lpThreadParameter)
{
printf("adfasdfasdfasdf\n");
return 0;
}
DWORD WINAPI receive(LPVOID lpThreadParameter)
{
//初始化WSA
WORD sockVersion = MAKEWORD(2,2);
WSADATA wsaData;
// unsigned long flag;
// flag=0;
if(WSAStartup(sockVersion, &wsaData)!=0)
{
return 0;
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(slisten == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
// if(ioctlsocket(slisten,FIONBIO, &flag)!=0)
// printf("fei zu se shibai \n");
//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("bind error !");
}
while(1)
{
if(listen(slisten, 5) == SOCKET_ERROR)
{
printf("listen error !");
return 0;
}
//循环接收数据
SOCKET sClient;
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[255];
//开始监听
// printf("..............\n");
sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
if(sClient == INVALID_SOCKET)
{
printf("accept error !");
}
// printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
//接收数据
int ret = recv(sClient, revData, 255, 0);
if(ret > 0)
{
revData[ret] = 0x00;
printf("YOU :");
printf(revData);
printf("\n");
}
}
closesocket(slisten);
WSACleanup();
return 0;
}
DWORD WINAPI send(LPVOID lpThreadParameter)
{
char sdata[1000];
WORD sockVersion = MAKEWORD(2,2);
WSADATA data;
unsigned long flag;
flag=0;
if(WSAStartup(sockVersion, &data) != 0)
{
return 0;
}
while(1)
{
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sclient == INVALID_SOCKET)
{
printf("invalid socket !");
return 0;
}
//非阻塞
if(ioctlsocket(sclient,FIONBIO, &flag)!=0)
printf("fei zu se shibai \n");
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8889);
serAddr.sin_addr.S_un.S_addr = inet_addr("169.254.244.91");
if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("connect error !");
closesocket(sclient);
return 0;
}
memset(sdata,0,1000);
printf("I :");
scanf("%s",sdata);
send(sclient, sdata, strlen(sdata), 0);
closesocket(sclient);
}
WSACleanup();
return 0;
}
int main()
{
QueueUserWorkItem(send, (void*)1 , 0x00000020);
QueueUserWorkItem(receive, (void*)1 ,0x00000020);
Sleep(100000);
return 0;
}