Win_connect交流程序
继上次cmd黑框框升级版
#include <winsock2.h>
#include <windows.h>
#include <windowsx.h>
#include <cstring>
#include <cstdlib>
#include <time.h>
#include <tchar.h>
#pragma comment (lib,"ws2_32.lib")
#define Con const
#define W_SIZE 650
#define TIME_LEN 20
#define IP_LEN 20
#define STR_MAX_LEN 200
#define MEM_STR_LEN 5000
#define NAME_LEN 15
#define IN_TEXT 101
#define OUT_TEXT 201
#define B_DOWN 301
using namespace std;
char sendbuf[STR_MAX_LEN],
recvbuf[STR_MAX_LEN],
memobuf[MEM_STR_LEN];
char time_l[TIME_LEN], time_n[TIME_LEN];
const char I_name[NAME_LEN] = "世羽";
char Y_name[NAME_LEN];
char o_IP[IP_LEN];
bool beginipin = 0 , recvinname = 0 , outlock = 0 ;
SOCKET connection;
void hwndOutput_add_buf(HWND, Con char*, bool n = 1);
//加密函数
void outres(char* buf) {
int len = strlen(buf);
for(char* i = buf;i != buf + len;i++) (*i)++;
}
//解密函数
void inres(char* buf) {
int len = strlen(buf);
for(char* i = buf;i != buf + len;i++) (*i)--;
}
DWORD WINAPI show_time(LPVOID hwndout){
HWND hwndOut = (HWND) hwndout;
while(1){
time_t timep; time(&timep);
strftime(time_n, sizeof(time_n), "%Y-%m-%d %H:%M", localtime(&timep));
if (strcmp(time_l, time_n)) {
strcpy(time_l, time_n);
hwndOutput_add_buf(hwndOut, time_n);
}
}
}
char* cinIP(HWND hwndin, HWND hwndout){
GetWindowText(hwndin, o_IP, IP_LEN);
SetWindowText(hwndin, "");
Edit_LimitText(hwndin, 0);
strcat(sendbuf, "目标/自身 地址:: ");
strcat(sendbuf, o_IP);
hwndOutput_add_buf(hwndout, sendbuf);
*sendbuf = '\0';
return o_IP;
}
SOCKET gotsock(HWND hwndmind) {
WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
SOCKET user_i, user_o;
sockaddr_in addr, addr_f;
addr.sin_family = AF_INET;
addr.sin_port = htons(14707);
addr.sin_addr.S_un.S_addr = inet_addr(o_IP);
user_i = socket(AF_INET, SOCK_STREAM, 0);
user_o = socket(AF_INET, SOCK_STREAM, 0);
if (!connect(user_o, (sockaddr*)&addr, sizeof(addr))){
hwndOutput_add_buf(hwndmind, "目标连接成功");
return user_o;
}
if (bind(user_i, (sockaddr*)&addr, sizeof(addr)) != 0)
return user_o;
listen(user_i, 1);
int len = sizeof(sockaddr);
hwndOutput_add_buf(hwndmind, "等待连接(此期间程序不会响应任何操作)......");
user_o = accept(user_i, (sockaddr*)&addr_f, &len);
hwndOutput_add_buf(hwndmind, "已被链接上");
return user_o;
}
void w_send(HWND hwndin, HWND hwndlog){
static char showbuf[STR_MAX_LEN + NAME_LEN + 2];
GetWindowText(hwndin, sendbuf, STR_MAX_LEN);
strcat(sendbuf, ".");
SetWindowText(hwndin, "");
outres(sendbuf);
send(connection, sendbuf, STR_MAX_LEN, 0);
inres(sendbuf);
strcpy(showbuf, I_name);
strcat(showbuf, ": ");
strcat(showbuf, sendbuf);
hwndOutput_add_buf(hwndlog, showbuf);
}
DWORD WINAPI w_recv(LPVOID hwndout) {
HWND hwndOut = (HWND) hwndout;
recv(connection, Y_name, NAME_LEN, 0);
char showbuf[STR_MAX_LEN + NAME_LEN + 2];
unsigned short noxtime = 0;
while(noxtime < 3) {
recv(connection, recvbuf, STR_MAX_LEN, 0);
inres(recvbuf);
if(*recvbuf) { noxtime = 0;
strcpy(showbuf, Y_name);
strcat(showbuf, ": ");
strcat(showbuf, recvbuf);
hwndOutput_add_buf(hwndOut, showbuf);
} else noxtime++;
*recvbuf = '\0';
outres(recvbuf);
} hwndOutput_add_buf(hwndOut, "对方已下线,接收线程退出");
return 0;
}
void hwndOutput_add_buf(HWND hwnd, Con char* buf, bool n) {
while(outlock);
outlock = 1;
GetWindowText(hwnd, memobuf, MEM_STR_LEN - STR_MAX_LEN - 10);
strcat(memobuf, buf);
if(n) strcat(memobuf, "\r\n");
SetWindowText(hwnd, memobuf);
SendMessage(hwnd, EM_LINESCROLL,
0, Edit_GetLineCount(hwnd));
outlock = 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
static HDC hdc;
static PAINTSTRUCT ps;
static HANDLE wc_listen,
wc_time;
static DWORD wc_listenID = 0,
wc_timeID = 0;
static HWND B_send;
static HWND hwndOutput,
hwndInput;
static int wmID,
wmEvent;
switch(Message) {
case WM_CREATE:{
unsigned int std_l = W_SIZE * 0.618 - 83;
hwndOutput = CreateWindow( TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0, W_SIZE - 20, std_l,
hwnd, (HMENU)OUT_TEXT, ((LPCREATESTRUCT) lParam) -> hInstance, NULL );
hwndInput = CreateWindow( TEXT("edit"), NULL,
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_BORDER |
ES_LEFT | ES_AUTOHSCROLL,
0, std_l, W_SIZE - 70, 40,
hwnd, (HMENU)IN_TEXT, ((LPCREATESTRUCT) lParam) -> hInstance, NULL );
HWND B_send=CreateWindow( TEXT("BUTTON"), TEXT("Send"),
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
W_SIZE - 70, std_l, 50, 40,
hwnd, (HMENU)B_DOWN, (HINSTANCE)GetWindowLong(hwnd, -6), NULL);
Edit_LimitText(hwndInput, IP_LEN);
strcpy(memobuf, "输入 目标(客户端)/自己(服务端) IP:\r\n");
SetWindowText(hwndOutput, memobuf);
SetWindowText(hwndInput, "xxx.xxx.xxx.xxx");
break;
}
case WM_PAINT:{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
}
case WM_COMMAND: {
wmID = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch(wmID) {
case B_DOWN: {
if(!beginipin) {
cinIP(hwndInput, hwndOutput);
connection = gotsock(hwndOutput);
beginipin = true;
send(connection, I_name, NAME_LEN, 0);
Edit_LimitText(hwndInput, STR_MAX_LEN);
wc_listen = (HANDLE)::CreateThread(NULL, 0,
w_recv, (LPVOID)hwndOutput, 0, &wc_listenID);
wc_time = (HANDLE)::CreateThread(NULL, 0,
show_time, (LPVOID)hwndOutput, 0, &wc_timeID);
break;
}
w_send(hwndInput, hwndOutput);
break;
}
}
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "Win_connect";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Win_connect", "connect", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
W_SIZE, /* width */
W_SIZE * 0.618, /* height */
NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}