求一个Socket(vc++)通讯的简单原代码,不胜感激!邮箱:zyk811129@163.com

qinguangjun123 2006-11-01 01:12:17
求一个Socket(vc++)通讯的简单原代码,不胜感激!
谢谢!
邮箱:zyk811129@163.com
...全文
405 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
无知者无谓 2006-11-17
  • 打赏
  • 举报
回复
到codeproject上一抓一大把
Seu_why 2006-11-01
  • 打赏
  • 举报
回复
vckbase.com 自己去看
yangyi198381 2006-11-01
  • 打赏
  • 举报
回复
好的,我发给你,记得给我分哦!!!
qinguangjun123 2006-11-01
  • 打赏
  • 举报
回复
谢谢,不想用这种方式。
Y___Y 2006-11-01
  • 打赏
  • 举报
回复
客户端:
// Client.cpp : Defines the entry point for the application.
#include <winsock2.h>
#include <windows.h>

#define MAX_LOADSTRING 100
#define ID_REIVEEDIT 1
#define ID_SENDEDIT 2
#define SERVER_ADDR "202.114.169.172"
#define SOCKET_PORT 5111
#define WM_SOCKET (WM_USER+1)
// Foward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

// Initialize global strings
TCHAR szTitle[MAX_LOADSTRING]=TEXT("Client");
TCHAR szWindowClass[MAX_LOADSTRING]=TEXT("ClientClass");
MSG msg;
HWND hWnd;
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
// Perform application initialization:
if (!RegisterClass(&wcex))
{
MessageBox(NULL,"Error!","Server",MB_ICONERROR|MB_OK);
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

HINSTANCE hIn;
static HWND ReEdit,SeEdit;
int nSize;
WSADATA WSAData;
static struct sockaddr_in server,from;
static SOCKET remote_ser;
int nError;
WORD wEvent,wError;
static TCHAR szRe[1024],szSe[1024];
int SOCKET_ERROR;
switch (message)
{
case WM_CREATE:
hIn=(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
//创建两个编辑框
ReEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_DISABLED|WS_HSCROLL
|WS_VSCROLL|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT,
0,0,500,100,hWnd,(HMENU)ID_REIVEEDIT,hIn,NULL);
SeEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_HSCROLL
|WS_VSCROLL|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT,
0,100,500,100,hWnd,(HMENU)ID_SENDEDIT,hIn,NULL);
//初始化套接字
if(nError=WSAStartup(MAKEWORD(2,0),&WSAData))
{
MessageBox(hWnd,"WinSock Startup Error!","Server",MB_ICONERROR|MB_OK);
return true;
}
//创建套接字
remote_ser=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(remote_ser==INVALID_SOCKET)
{
MessageBox(hWnd,"Socket Creation Error!","Server",MB_ICONERROR|MB_OK);
WSACleanup();
return true;
}
if(SOCKET_ERROR==WSAAsyncSelect(remote_ser,hWnd,WM_SOCKET,FD_CONNECT|FD_READ))
{
MessageBox(hWnd,"WSAAsyncSelect Error!","Server",MB_ICONERROR|MB_OK);
WSACleanup();
return true;
}
server.sin_family=AF_INET;
server.sin_port=htons(SOCKET_PORT);
server.sin_addr.S_un.S_addr=inet_addr(SERVER_ADDR);
connect(remote_ser,(struct sockaddr *)&server,sizeof(server));
if(WSAGetLastError()!=WSAEWOULDBLOCK)
{
MessageBox(hWnd,"Connect Error!","Server",MB_ICONERROR|MB_OK);
closesocket(remote_ser);
WSACleanup();
return 0;
}
return 0;
case WM_SOCKET:
wEvent=WSAGETSELECTEVENT(lParam);
wError=WSAGETSELECTERROR(lParam);
switch(wEvent)
{
case FD_CONNECT:
if(wError)
{
MessageBox(hWnd,"Accept Error!","Server",MB_ICONERROR|MB_OK);
return 0;
}
nSize=sizeof(from);
EnableWindow(ReEdit,true);
SetWindowText(ReEdit,"已经连接上服务器");
return 0;
case FD_READ:
if(wError)
{
MessageBox(hWnd,"Read Error!","Server",MB_ICONERROR|MB_OK);
return 0;
}
szRe[0]='\0';
recv(remote_ser,szRe,sizeof(szRe)-1,0);
SetWindowText(ReEdit,szRe);
return 0;
}
return 0;
case WM_RBUTTONDOWN:
GetWindowText(SeEdit,szSe,GetWindowTextLength(SeEdit)+1);
send(remote_ser,szSe,strlen(szSe)+1,0);
SetWindowText(SeEdit,"");
return 0;
case WM_SETFOCUS:
SetFocus(SeEdit);
return 0;
case WM_COMMAND:
if(LOWORD(wParam)==ID_SENDEDIT)
if(HIWORD(wParam)==EN_CHANGE)
{
//GetWindowText(SeEdit,szSe,GetWindowTextLength(SeEdit)+1);
//send(local_listen,szSe,strlen(szSe)+1,0);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
closesocket(remote_ser);
WSACleanup();
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Y___Y 2006-11-01
  • 打赏
  • 举报
回复
下面的是用非mfc编写的

服务器端:
// Server.cpp : Defines the entry point for the application.
#include <winsock2.h>
#include <windows.h>

#define MAX_LOADSTRING 100
#define ID_REIVEEDIT 1
#define ID_SENDEDIT 2
#define SOCKET_PORT 5111
#define WM_SOCKET (WM_USER+1)
// Foward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

// Initialize global strings
TCHAR szTitle[MAX_LOADSTRING]=TEXT("Server");
TCHAR szWindowClass[MAX_LOADSTRING]=TEXT("ServerClass");
MSG msg;
HWND hWnd;
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
// Perform application initialization:
if (!RegisterClass(&wcex))
{
MessageBox(NULL,"Error!","Server",MB_ICONERROR|MB_OK);
return FALSE;
}
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

HINSTANCE hIn;
static HWND ReEdit,SeEdit;
int nSize;
WSADATA WSAData;
static struct sockaddr_in server,from;
static SOCKET local_ser,local_listen;
int nError;
WORD wEvent,wError;
static TCHAR szRe[1024],szSe[1024];
char szadd[256];
int SOCKET_ERROR;
switch (message)
{
case WM_CREATE:
hIn=(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
//创建两个编辑框
ReEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_DISABLED|WS_HSCROLL
|WS_VSCROLL|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT,
0,0,500,100,hWnd,(HMENU)ID_REIVEEDIT,hIn,NULL);
SeEdit=CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_HSCROLL
|WS_VSCROLL|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_LEFT,
0,100,500,100,hWnd,(HMENU)ID_SENDEDIT,hIn,NULL);
//初始化套接字
if(nError=WSAStartup(MAKEWORD(2,0),&WSAData))
{
MessageBox(hWnd,"WinSock Startup Error!","Server",MB_ICONERROR|MB_OK);
return true;
}
//创建套接字
local_ser=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(local_ser==INVALID_SOCKET)
{
MessageBox(hWnd,"Socket Creation Error!","Server",MB_ICONERROR|MB_OK);
WSACleanup();
return true;
}
if(SOCKET_ERROR==WSAAsyncSelect(local_ser,hWnd,WM_SOCKET,FD_ACCEPT|FD_READ))
{
MessageBox(hWnd,"WSAAsyncSelect Error!","Server",MB_ICONERROR|MB_OK);
WSACleanup();
return true;
}
server.sin_family=AF_INET;
server.sin_port=htons(SOCKET_PORT);
if(bind(local_ser,(struct sockaddr *)&server,sizeof(server)))
{
MessageBox(hWnd,"Bind Error!","Server",MB_ICONERROR|MB_OK);
closesocket(local_ser);
WSACleanup();
return true;
}
listen(local_ser,5);
return 0;
case WM_SOCKET:
wEvent=WSAGETSELECTEVENT(lParam);
wError=WSAGETSELECTERROR(lParam);
switch(wEvent)
{
case FD_ACCEPT:
if(wError)
{
MessageBox(hWnd,"Accept Error!","Server",MB_ICONERROR|MB_OK);
return 0;
}
nSize=sizeof(from);
local_listen=accept(local_ser,(struct sockaddr *)&from,&nSize);
EnableWindow(ReEdit,true);
strcpy(szadd,inet_ntoa(from.sin_addr));
strcat(szadd,"已经连接上了!");
SetWindowText(ReEdit,szadd);
return 0;
case FD_READ:
if(wError)
{
MessageBox(hWnd,"Read Error!","Server",MB_ICONERROR|MB_OK);
return 0;
}
szRe[0]='\0';
recv(local_listen,szRe,sizeof(szRe)-1,0);
SetWindowText(ReEdit,szRe);
return 0;
}
return 0;
case WM_RBUTTONDOWN:
GetWindowText(SeEdit,szSe,GetWindowTextLength(SeEdit)+1);
send(local_listen,szSe,strlen(szSe)+1,0);
SetWindowText(SeEdit,"");
return 0;
case WM_SETFOCUS:
SetFocus(SeEdit);
return 0;
case WM_COMMAND:
if(LOWORD(wParam)==ID_SENDEDIT)
if(HIWORD(wParam)==EN_CHANGE)
{
//GetWindowText(SeEdit,szSe,GetWindowTextLength(SeEdit)+1);
//send(local_listen,szSe,strlen(szSe)+1,0);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
//DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
closesocket(local_ser);
closesocket(local_listen);
WSACleanup();
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧