/******************************************************************************************************/
//更改接收端端口ip(RecvPort )和发送端端口ip (SendPort )即可互联(7000-》6000,6000-》)
//设置窗口隐藏
/******************************************************************************************************/
#include<WINSOCK2.H>
#include<stdio.h>
#include<time.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
//#pragma comment( linker, "/subsystem:"windows" /entry:"mainCRTStartup"") //-- 对应main函数(隐藏窗口)
#pragma warning(disable:4996)
DWORD WINAPI thread_recv(PVOID pParam);
DWORD WINAPI thread_send(PVOID pParam);
DWORD WINAPI thread_accept(PVOID pParam);
const int SendPort = 7000;
const int RecvPort = 6000;
const char* SendIp = "127.0.0.1";
const char* RecvdIp = "127.0.0.1";
#define BUFFER_SIZE 1024 //注意:此Server端数据接收缓冲区 >= Client端数据发送缓冲区 ,否则造成缓冲区溢出
struct THreadRecv { SOCKET ser; };
struct THreadSend { SOCKET ser; HANDLE Handle; char buf[100]; };
struct THreadAccept { SOCKET ser; };
int main(int argc,char *argv[])
{
//ShowWindow(FindWindow("ConsoleWindowClass",argv[0]),0); //查找窗口隐藏自身 ..
//MessageBox(NULL,TEXT("您选择了NO"),TEXT("NO"),MB_OK);
HANDLE Handle_accept = CreateThread(NULL, 0, thread_accept, NULL, 0, NULL); //创建接收端
HANDLE Handle_send = CreateThread(NULL, 0, thread_send, NULL, 0, NULL); //创建发送端
while(true){
}
printf("结束程序");
return 0;
}
//创建线程连接
DWORD WINAPI thread_accept(PVOID pParam) {
WORD version;
WSADATA data;
version = MAKEWORD(1, 1);
int err;
err = WSAStartup(version, &data);
if (!err)
{
printf("套接字一打开\n");
}
SOCKET ser = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //ip地址 INADDR_ANY指接收任意数据
addr.sin_port = htons(RecvPort); //端口6000
bind(ser, (SOCKADDR*)&addr, sizeof(SOCKADDR)); //绑定端口
listen(ser, 10); //最大连接数
printf("开始监听\n");
//绑定开始监听
//SOCKET ser;
SOCKADDR_IN client;
int len = sizeof(SOCKADDR);
//THreadAccept* acceptParam = (THreadAccept*)pParam;//获取参数结构体ss
//ser = acceptParam->ser;
THreadRecv * recvParam = new THreadRecv();
while (true)
{
SOCKET connect = accept(ser, (SOCKADDR*)&client, &len);
recvParam ->ser=connect;
//SOCKET connect = accept(ser, (SOCKADDR*)&client, &len);
HANDLE Handle = CreateThread(NULL, 0, thread_recv, recvParam, 0, NULL); //创建接收线程
}
return 0;
}
//创建线程接收消息
DWORD WINAPI thread_recv(PVOID pParam) {
printf("接收消息\n");
//绑定开始监听
SOCKET connect;
THreadRecv* recvParam = (THreadRecv*)pParam;//获取参数结构体ss
connect = recvParam->ser;
while (true)
{
char buf[BUFFER_SIZE];
memset(buf,0,sizeof(buf)); //初始化数据接收缓冲区
int length;
//最后一个参数一般设置为0
length = recv(connect, buf, BUFFER_SIZE, 0);
if (length != -1){
printf("对方说:%s\n", buf);
}
//if(buf=="dir") system(buf);
}
return 0;
}
//创建线程发送消息
DWORD WINAPI thread_send(PVOID pParam) {
printf("启动发送线程\n");
int err;
WORD verson;
WSADATA data;
verson = MAKEWORD(1, 1);
err = WSAStartup(verson, &data);
//system("pause");
SOCKET client = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN in;
in.sin_addr.S_un.S_addr = inet_addr(SendIp);
in.sin_family = AF_INET;
in.sin_port = htons(SendPort);
int i = -1;
bool myconn = false;
while (i == -1) //连接失败
{
i = connect(client, (SOCKADDR*)&in, sizeof(SOCKADDR)); //套接字连接成功
if (i == -1) {
printf("socket连接失败,是否重连(y/n)\n");
if (getchar() != 'y') i = 0;
Sleep(1000);
}
else{
printf("连接成功\n");
myconn = true;
i = 0;
}
}
char sendchar[1000] = {};
//char * sendchar = {};
//char sendchar;
time_t timep;
time(&timep);
if (myconn == true){
printf("请输入消息\n");
while (true){
fflush(stdin); //清除缓存
scanf("%s", sendchar); //输入字符串。
int sendback = send(client, sendchar, strlen(sendchar) + 1, 0);
if (sendback > 0) {
// printf("消息发送成功,返回值为:%d\n", sendback);
printf("%s",ctime(&timep));
//printf("消息发送成功,返回值为:%d\n", sendback);
}
else{ printf("消息发送失败\n"); }
//system("pause");
}
}
closesocket(client); //关闭套接字连接
printf("消息发送线程关闭\n");
return 0;
}