| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1628 人关注过本帖
标题:[求助]如何用C++编写Http客户端程序,实现发送字符串功能
只看楼主 加入收藏
adrian518
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-10-24
收藏
 问题点数:0 回复次数:1 
[求助]如何用C++编写Http客户端程序,实现发送字符串功能
以前一直用C++单机编程,最近学了C#后对C++的网络编程产生了兴趣,但是没找到适合的书(高手给我推荐一本吧,呵呵);
我希望能通过这个问题揭开C++网络应用的冰山一角,进而完善C++使用能力,先谢谢大家了!!!
附:如果高手有时间的话再给我一个简单的Http服务端程序,实现接受Http请求报文就行了,我会感激不尽的!!!
搜索更多相关主题的帖子: Http 客户端 网络 字符 
2006-10-24 08:34
radcat
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:306
专家分:45
注 册:2006-9-12
收藏
得分:0 

没太听懂你的意思,这是我写的一个聊天程序,有发送和接收端.

// 928.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <winsock2.h>
#include <process.h>
#include <stdio.h>

#define DEFAULT_COUNT 20
#define DEFAULT_PORT 5150
#define DEFAULT_BUFFER 4096
#define MAX_LOADSTRING 100
static TCHAR DEFAULT_MESSAGE[100];

TCHAR SERVER[MAX_LOADSTRING]; // Server to connect to

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

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

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MY928, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY928);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MY928);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MY928;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

//接收函数部分

typedef struct _THREADDATA
{
SOCKET sock;
HWND hWnd;
} THREADDATA, *LPTHREADDATA;

DWORD WINAPI ClientThread(LPVOID lpParam)
{
SOCKET sock=((LPTHREADDATA)lpParam)->sock;
char szBuff[DEFAULT_BUFFER];
int ret,
nLeft,
idx;
char szMsg[255];
HWND hWnd = ((LPTHREADDATA)lpParam)->hWnd;

while(1)
{
// Perform a blocking recv() call
//
ret = recv(sock, szBuff, DEFAULT_BUFFER, 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;
}
szBuff[ret] = '\0';
wsprintf(szMsg,"%s", szBuff);

// add the szBuff 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);*/
SetDlgItemText(hWnd,IDC_EDIT2,szMsg);

nLeft = ret;
idx = 0;
//
// Make sure we write all the data
//
while(nLeft > 0)
{
ret = send(sock, &szBuff[idx], nLeft, 0);
if (ret == 0)
break;
else if (ret == SOCKET_ERROR)
{
wsprintf(szMsg, "send()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK |MB_ICONSTOP);
break;
}
nLeft -= ret;
idx += ret;
}
}
return 0;
}

//
// Function: Process
//
// Description:
// This function is called as a thread. It initialize
// Winsock, create the listening socket, bind to the
// local address, and wait for client connections.
//
DWORD WINAPI Process(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
char szMsg[255];
SOCKET sListen,
sClient;
int iAddrSize;
HANDLE hThread;
DWORD dwThreadId;
struct sockaddr_in local,
client;
THREADDATA data;

if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
MessageBox(hWnd, "不能加载Winsock!",
"", MB_OK | MB_ICONSTOP);
return 1;
}
// Create our listening socket
//
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (sListen == SOCKET_ERROR)
{
wsprintf(szMsg, "socket()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
// Set the local interface and bind to it
//
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(DEFAULT_PORT);

if (bind(sListen, (struct sockaddr *)&local,
sizeof(local)) == SOCKET_ERROR)
{
wsprintf(szMsg, "bind()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
listen(sListen, 8);
//
// In a continous loop, wait for incoming clients. Once one
// is detected, create a thread and pass the handle off to it.
//
while (1)
{
iAddrSize = sizeof(client);
sClient = accept(sListen, (struct sockaddr *)&client,
&iAddrSize);
if (sClient == INVALID_SOCKET)
{
wsprintf(szMsg, "accept()失败: %d",
WSAGetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
wsprintf(szMsg, "已接受客户: %s:%d",
inet_ntoa(client.sin_addr), ntohs(client.sin_port));

// 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);

// set the ClientThread required data
//
data.hWnd = hWnd;
data.sock = sClient;

hThread = CreateThread(NULL, 0, ClientThread,
(LPVOID)&data, 0, &dwThreadId);
if (hThread == NULL)
{
wsprintf(szMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
break;
}
CloseHandle(hThread);
}
closesocket(sListen);

WSACleanup();
return 0;
}


//发送函数

DWORD WINAPI Processfrom(LPVOID lpParam)
{
HWND hWnd = (HWND)lpParam;
WSADATA wsd;
SOCKET sClient;
char szBuffer[DEFAULT_BUFFER];
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;
}
server.sin_family = AF_INET;
server.sin_port = htons(DEFAULT_PORT);
server.sin_addr.s_addr = inet_addr(SERVER);
//
// If the supplied server address wasn't in the form
// "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it
//
if (server.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(SERVER);
if (host == NULL)
{
wsprintf(szMsg, "不能解析服务器: %s", SERVER);
MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
return 1;
}
CopyMemory(&server.sin_addr, host->h_addr_list[0],
host->h_length);
}
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, DEFAULT_BUFFER, 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 About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hcomb,hedit;
HANDLE hThread,hThreadfrom;
DWORD dwThreadId;
int i;

switch (message)
{
case WM_INITDIALOG:
hcomb=GetDlgItem(hDlg,IDC_COMBO1);
SendMessage(hcomb,CB_ADDSTRING,0,(LPARAM)TEXT("vr-01"));
SendMessage(hcomb,CB_ADDSTRING,0,(LPARAM)TEXT("vr-02"));
SendMessage(hcomb,CB_ADDSTRING,0,(LPARAM)TEXT("vr-03"));
SendMessage(hcomb,CB_ADDSTRING,0,(LPARAM)TEXT("vr-04"));
SendMessage(hcomb,CB_ADDSTRING,0,(LPARAM)TEXT("vr-05"));

hThread = CreateThread(NULL, 0, Process,
(LPVOID)hDlg, 0, &dwThreadId);
if (hThread == NULL)
{
char szErrMsg[255];
wsprintf(szErrMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hDlg, szErrMsg, "",
MB_OK | MB_ICONSTOP);
}
CloseHandle(hThread);


return TRUE;

case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
GetDlgItemText(hDlg,IDC_EDIT3,DEFAULT_MESSAGE,
100);
hThreadfrom = CreateThread(NULL, 0, Processfrom,
(LPVOID)hDlg, 0, &dwThreadId);
if (hThreadfrom == NULL)
{
char szErrMsg[255];
wsprintf(szErrMsg, "CreateThread()失败: %d",
GetLastError());
MessageBox(hDlg, szErrMsg, "",
MB_OK | MB_ICONSTOP);
}
CloseHandle(hThreadfrom );
SetDlgItemText(hDlg,IDC_EDIT3,NULL);
hedit=GetDlgItem(hDlg,IDC_EDIT3);
SetFocus(hedit);
break;
case IDC_COMBO1:
switch(HIWORD(wParam))
{
case CBN_SELCHANGE:
hcomb=GetDlgItem(hDlg,IDC_COMBO1);
i=SendMessage(hcomb, CB_GETCURSEL, 0, 0);
if(i==0)
LoadString(hInst, IDS_STRING110, SERVER, MAX_LOADSTRING);
if(i==1)
LoadString(hInst, IDS_STRING111, SERVER, MAX_LOADSTRING);
if(i==2)
LoadString(hInst, IDS_STRING112, SERVER, MAX_LOADSTRING);
if(i==3)
LoadString(hInst, IDS_STRING113, SERVER, MAX_LOADSTRING);
if(i==4)
LoadString(hInst, IDS_STRING114, SERVER, MAX_LOADSTRING);

break;
}
break;
case IDCANCEL:
PostQuitMessage(0);
break;
}
break;
}
return FALSE;
}


2006-10-24 14:19
快速回复:[求助]如何用C++编写Http客户端程序,实现发送字符串功能
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029260 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved