代码:
#include <winsock2.h>
#include "resource.h"
#include "StdControl1.h"#define DEFAULT_COUNT 20 // Number of times to send message#define DEFAULT_MESSAGE "这是一个紧急广播系统试验" // Message to
// send to severLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
char ip[255];
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
char szClassName[] = "TCPClient"; wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndClass.lpszClassName = szClassName; RegisterClass(&wndClass); hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
szClassName,
"TCP客户示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd); while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;} // WinMain//
// Function: Process
//
// Description:
// This function is called as a thread. It initialize Winsock,
// create a socket, connect to the server, and then send and
// receive data.
//
DWORD WINAPI Process(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
SOCKET sClient;
char szBuffer[1023],FullName[255];
int ret;
DWORD i;
struct sockaddr_in server;
struct hostent *host = NULL;
char szMsg[255]; if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock库!", "",
MB_OK | MB_ICONSTOP);
return 1;
}
//
// Create the socket, and attempt to connect to the server
//
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "socket()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
gethostname(FullName,strlen(FullName));
host = gethostbyname(FullName);
server.sin_family = AF_INET;
server.sin_port = htons(2121);
server.sin_addr.s_addr = inet_addr(ip);
if (connect(sClient, (struct sockaddr *)&server,
sizeof(server)) == SOCKET_ERROR)
{
wsprintf(szMsg, "connect()失败: %d", WSAGetLastError());
return 1;
}
// Send and receive data
//
for(i = 0; i < DEFAULT_COUNT; i++)
{
ret = send(sClient, DEFAULT_MESSAGE,
lstrlen(DEFAULT_MESSAGE), 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "发送了%d字节", ret); // add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0); ret = recv(sClient, szBuffer, 1023, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuffer[ret] = '\0';
wsprintf(szMsg, "RECV [%d 字节]: '%s'", ret, szBuffer); //add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
}
closesocket(sClient); WSACleanup();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndListBox;
static HINSTANCE hInst ;
COLORREF bkColor ;
static HWND hwndButton1, hwndButton2;
static HWND hwndedit1, hwndedit2;
HANDLE hThread; // thread handle
DWORD dwThreadId; // thread identity
int i; switch(iMsg)
{
case WM_CREATE: // create the ListBox window
bkColor = (COLORREF) GetSysColor(COLOR_BTNFACE);
SetClassLong(hWnd, GCL_HBRBACKGROUND, (long)CreateSolidBrush(bkColor)); hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE) ;
hwndButton1 = CreateWindow( "BUTTON", "请确认(&C)", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
80, 300, 100, 30, hWnd, (HMENU)IDC_MYOK, hInst, NULL); hwndButton2 = CreateWindow( "BUTTON", "关闭(&Q)", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON ,
300, 300, 100, 30, hWnd, (HMENU)IDC_MYCANCEL, hInst, NULL); hwndedit1 = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT | \
ES_MULTILINE | ES_WANTRETURN ,
530, 80, 40, 30, hWnd, (HMENU) IDC_MYEDIT,
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
hwndedit2 = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT | \
ES_MULTILINE | ES_WANTRETURN ,
530, 185, 40, 30, hWnd, (HMENU) IDC_MYEDIT,
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
return 0; case WM_SIZE: // size the ListBox to fill client area
MoveWindow(hwndListBox, 0, 0, LOWORD(lParam),
HIWORD(lParam), FALSE);
return 0; case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_SEND: // create a thread to send data
hThread = CreateThread(NULL, 0, Process,
(LPVOID)hWnd, 0, &dwThreadId);
if (hThread == NULL)
{
char szErrMsg[255];
wsprintf(szErrMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hWnd, szErrMsg, "",
MB_OK | MB_ICONSTOP);
}
CloseHandle(hThread);
break;
case IDM_CONNECT: // clear the content of ListBox
DialogBox(hInst,MAKEINTRESOURCE(IDD_IP),hWnd,AboutDlgProc);
return 0; case WM_DESTROY:
PostQuitMessage(0);
return 0; default:
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
} // WndProc BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
}--------------------Configuration: Client - Win32 Release--------------------
Compiling...
Client.c
E:\专业相关\网络与通信\game_21point\CLIENT\Client.c(256) : error C2275: 'BOOL' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\windef.h(142) : see declaration of 'BOOL'
E:\专业相关\网络与通信\game_21point\CLIENT\Client.c(256) : error C2143: syntax error : missing ';' before '__stdcall'
Error executing cl.exe.Client.obj - 2 error(s), 0 warning(s)还没写完, 但刚刚加了个对话筐后,出现这个问题 想了半天都想不出为什么? 和编译通过的对话框例子比较也没什么不同? 请高手指点! 谢谢
#include <winsock2.h>
#include "resource.h"
#include "StdControl1.h"#define DEFAULT_COUNT 20 // Number of times to send message#define DEFAULT_MESSAGE "这是一个紧急广播系统试验" // Message to
// send to severLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
char ip[255];
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
char szClassName[] = "TCPClient"; wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wndClass.lpszClassName = szClassName; RegisterClass(&wndClass); hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
szClassName,
"TCP客户示例程序",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd); while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;} // WinMain//
// Function: Process
//
// Description:
// This function is called as a thread. It initialize Winsock,
// create a socket, connect to the server, and then send and
// receive data.
//
DWORD WINAPI Process(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
SOCKET sClient;
char szBuffer[1023],FullName[255];
int ret;
DWORD i;
struct sockaddr_in server;
struct hostent *host = NULL;
char szMsg[255]; if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock库!", "",
MB_OK | MB_ICONSTOP);
return 1;
}
//
// Create the socket, and attempt to connect to the server
//
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "socket()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
gethostname(FullName,strlen(FullName));
host = gethostbyname(FullName);
server.sin_family = AF_INET;
server.sin_port = htons(2121);
server.sin_addr.s_addr = inet_addr(ip);
if (connect(sClient, (struct sockaddr *)&server,
sizeof(server)) == SOCKET_ERROR)
{
wsprintf(szMsg, "connect()失败: %d", WSAGetLastError());
return 1;
}
// Send and receive data
//
for(i = 0; i < DEFAULT_COUNT; i++)
{
ret = send(sClient, DEFAULT_MESSAGE,
lstrlen(DEFAULT_MESSAGE), 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "发送了%d字节", ret); // add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0); ret = recv(sClient, szBuffer, 1023, 0);
if (ret == 0) // Graceful close
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "recv()失败: %d", WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
szBuffer[ret] = '\0';
wsprintf(szMsg, "RECV [%d 字节]: '%s'", ret, szBuffer); //add the szMsg text to ListBox
//
SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
(LPARAM)szMsg);
SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
- 1, 0);
}
closesocket(sClient); WSACleanup();
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndListBox;
static HINSTANCE hInst ;
COLORREF bkColor ;
static HWND hwndButton1, hwndButton2;
static HWND hwndedit1, hwndedit2;
HANDLE hThread; // thread handle
DWORD dwThreadId; // thread identity
int i; switch(iMsg)
{
case WM_CREATE: // create the ListBox window
bkColor = (COLORREF) GetSysColor(COLOR_BTNFACE);
SetClassLong(hWnd, GCL_HBRBACKGROUND, (long)CreateSolidBrush(bkColor)); hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE) ;
hwndButton1 = CreateWindow( "BUTTON", "请确认(&C)", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
80, 300, 100, 30, hWnd, (HMENU)IDC_MYOK, hInst, NULL); hwndButton2 = CreateWindow( "BUTTON", "关闭(&Q)", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON ,
300, 300, 100, 30, hWnd, (HMENU)IDC_MYCANCEL, hInst, NULL); hwndedit1 = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT | \
ES_MULTILINE | ES_WANTRETURN ,
530, 80, 40, 30, hWnd, (HMENU) IDC_MYEDIT,
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
hwndedit2 = CreateWindow(TEXT("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | ES_LEFT | \
ES_MULTILINE | ES_WANTRETURN ,
530, 185, 40, 30, hWnd, (HMENU) IDC_MYEDIT,
(HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
return 0; case WM_SIZE: // size the ListBox to fill client area
MoveWindow(hwndListBox, 0, 0, LOWORD(lParam),
HIWORD(lParam), FALSE);
return 0; case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_SEND: // create a thread to send data
hThread = CreateThread(NULL, 0, Process,
(LPVOID)hWnd, 0, &dwThreadId);
if (hThread == NULL)
{
char szErrMsg[255];
wsprintf(szErrMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hWnd, szErrMsg, "",
MB_OK | MB_ICONSTOP);
}
CloseHandle(hThread);
break;
case IDM_CONNECT: // clear the content of ListBox
DialogBox(hInst,MAKEINTRESOURCE(IDD_IP),hWnd,AboutDlgProc);
return 0; case WM_DESTROY:
PostQuitMessage(0);
return 0; default:
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
} // WndProc BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
}--------------------Configuration: Client - Win32 Release--------------------
Compiling...
Client.c
E:\专业相关\网络与通信\game_21point\CLIENT\Client.c(256) : error C2275: 'BOOL' : illegal use of this type as an expression
C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\windef.h(142) : see declaration of 'BOOL'
E:\专业相关\网络与通信\game_21point\CLIENT\Client.c(256) : error C2143: syntax error : missing ';' before '__stdcall'
Error executing cl.exe.Client.obj - 2 error(s), 0 warning(s)还没写完, 但刚刚加了个对话筐后,出现这个问题 想了半天都想不出为什么? 和编译通过的对话框例子比较也没什么不同? 请高手指点! 谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货