|
/*********************************************************************/
/***************WINDOWS SOCKET代理服务器******************************/
/***************Email:tujj99@hotmail.com******************************/
/***************程序还有很多问题,欢迎讨论****************************/
/*********************************************************************/
#include <winsock2.h>
//#include <windows.h>
#include <stdio.h>
//#include <conio.h>
#define SERVER_PORT 4040 /*服务端口 */
#define PROXY_PORT 8080
#define MSG_SIZE 8192 /*缓冲区的大小 */
#define MAX_THREAD 1000
//#define SERVER_NAME_MAX_SIZE 256
#define BACKLOG 5 /*等待服务的最大数*/
char server_addr[]="172.17.1.39";
char proxy_addr[]="130.1.201.81";
int server_port = SERVER_PORT;
int proxy_port = PROXY_PORT;
int thread_no;
struct sockaddr_in pro_addr;
SOCKET hostserver;
unsigned long thID;
typedef struct tClientIP_
{
char ip[16];
unsigned short port;
SOCKET cli_socket;
int reserve;
}tClientIP;
/****************************************************************/
/* NAME : InitServer */
/* IN : NULL */
/* OUT : 1/0 Success/Failure */
/* Sumary: 初始化服务器 */
/****************************************************************/
int InitServer()
{
struct sockaddr_in ser_addr; /* 服务器的地址 */
WSADATA WSAData;
int addrlen,errorno;
thread_no = 0;
//init WSA
if(WSAStartup(MAKEWORD(2,2),&WSAData)){
printf("WSA start error!");
return -1;
}
/*创建连接的SOCKET */
hostserver = socket(AF_INET,SOCK_STREAM,0);
if(hostserver == INVALID_SOCKET){/*创建失败 */
errorno = WSAGetLastError();
printf("socker Error:%d\n",errorno);
WSACleanup();
return -1;
}
else{
// printf("Server socket creating successfully!\n");
}
/*初始化服务器地址*/
addrlen=sizeof(struct sockaddr_in);
ZeroMemory(&ser_addr,addrlen);
ser_addr.sin_family=AF_INET;
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY); //任何主机都可以访问
ser_addr.sin_port=htons(server_port);
if(bind(hostserver,(struct sockaddr *)&ser_addr,sizeof(ser_addr)) == SOCKET_ERROR){/*绑定失败 */
errorno = WSAGetLastError();
printf("Bind Error:%d\n",errorno);
closesocket(hostserver);
WSACleanup();
return -1;
}
else{
printf("bind socket to successfully!\n");
}
/*侦听客户端请求*/
if(listen(hostserver,SOMAXCONN) == SOCKET_ERROR ){
errorno = WSAGetLastError();
printf("Listen Error:%d\n",errorno);
closesocket(hostserver);
WSACleanup();
return -1;
}
else{
printf("Start listening on %s:%d....\n",server_addr,server_port);
}
/* 初始化代理服务器地址*/
addrlen=sizeof(struct sockaddr_in);
ZeroMemory(&pro_addr,addrlen);
pro_addr.sin_family=AF_INET;
pro_addr.sin_addr.s_addr=inet_addr(proxy_addr);
pro_addr.sin_port=htons(proxy_port);
return 0;
}
/*****************************************************************/
/* NAME : ServerThread */
/* IN : LPVOID lpParam */
/* OUT : DWORD */
/* Sumary: 服务器接受一条消息,然后发送到代理server, */
/* 再接收从代理server返回的数据,再送往client */
/*****************************************************************/
DWORD WINAPI ServerThread(LPVOID lpParam)
{
int n; /* 接受到的或发送的数据的字节数*/
// ret; /* select()返回值 */
char msg[MSG_SIZE]; /* 缓冲区*/
SOCKET cli_socket, /* 客户端SOCKET */
pro_socket; /* Proxy SOCKET */
tClientIP ClientIP;
int errorno;
// fd_set fdread;
// fd_set fdwrite;
ClientIP = *(reinterpret_cast<tclientip *="">(lpParam));
cli_socket = ClientIP.cli_socket;
if(cli_socket == INVALID_SOCKET)
{
printf("client socket error!\n");
ExitThread(0);
}
/* 客户端IP+端口 */
//printf("Request from %s:%d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
printf("begin thread %d....\n",GetCurrentThreadId());
/* 创建一个新Proxy socket */
pro_socket = socket(AF_INET,SOCK_STREAM,0);
if(pro_socket == INVALID_SOCKET){/*创建失败 */
errorno = WSAGetLastError();
printf("socker Error:%d\n",errorno);
thread_no --;
closesocket(cli_socket);
ExitThread(0);
}
else{
//printf("Proxy socket:%d creating successfully!\n",pro_socket);
}
int t = 2000;
if(setsockopt(pro_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&t, sizeof(t)) == SOCKET_ERROR)
{
errorno = WSAGetLastError();
printf("Set socket option Error:%d\n",errorno);
}
/* 请求连接代理服务器 */
if(connect(pro_socket,(struct sockaddr *)&pro_addr,sizeof(pro_addr)) == SOCKET_ERROR){
errorno = WSAGetLastError();
printf("Listen Error:%d\n",errorno);
thread_no --;
closesocket(pro_socket);
closesocket(cli_socket);
ExitThread(0);
}
// FD_ZERO(&fdread);
// FD_SET(cli_socket, &fdread);
// ret =select(NULL, &fdread, NULL, NULL, NULL);
// if(ret == SOCKET_ERROR)
// {
// printf("select error!%d\n",WSAGetLastError());
// closesocket(cli_socket);
// ExitThread(0);
// }
// if(FD_ISSET(cli_socket, &fdread)) {
while(1)
{
/* 接收客户端消息 */
ZeroMemory(msg,sizeof(msg));
n = recv(cli_socket,msg,MSG_SIZE,0);
if( n > 0){
printf("[%d]:C[%-16s:%-5d] >> S[%-16s:%-5d] [%d]\n",
GetCurrentThreadId(),ClientIP.ip,ClientIP.port,server_addr,server_port,n);
}
else if(n == 0) {
printf("client connection closed!\n");
break;
}
else if(n == SOCKET_ERROR){
errorno = WSAGetLastError();
printf("client socket closed or error:%d\n",errorno);
break;
}
// }
// FD_ZERO(&fdwrite);
// FD_SET(pro_socket, &fdwrite);
// ret = select(NULL, NULL, &fdwrite, NULL, NULL);
// if(ret == SOCKET_ERROR)
// {
// printf("select error!%d\n",WSAGetLastError());
// closesocket(cli_socket);
// ExitThread(0);
// }
// if(FD_ISSET(pro_socket, &fdwrite)) {
/* 转发消息到代理服务器*/
n = send(pro_socket,msg,n,0);
if( n > 0) {
// printf("[%d]:To Proxy ----> [%d]\n",GetCurrentThreadId(),n);
printf("[%d]:S[%-16s:%-5d] >> P[%-16s:%-5d] [%d]\n",
GetCurrentThreadId(),server_addr,server_port,proxy_addr,proxy_port,n);
}
else if(n == SOCKET_ERROR) {
errorno = WSAGetLastError();
printf("SEND TO PROXY ERROR:%d\n",errorno);
break;
}
// }
while(1)
{
// FD_ZERO(&fdread);
// FD_SET(pro_socket, &fdread);
// ret = select(NULL, &fdread, NULL, NULL, NULL);
// if(ret == SOCKET_ERROR)
// {
// printf("select error!%d\n",WSAGetLastError());
// closesocket(cli_socket);
// ExitThread(0);
// }
// if(FD_ISSET(pro_socket, &fdread)) {
/* 从代理服务器接收消息*/
ZeroMemory(msg,sizeof(msg));
n = recv(pro_socket,msg,MSG_SIZE,0);
if( n > 0){
// printf("[%d]:From Proxy <---- [%d]\n",GetCurrentThreadId(),n);
printf("[%d]:S[%-16s:%-5d] << P[%-16s:%-5d] [%d]\n",
GetCurrentThreadId(),server_addr,server_port,proxy_addr,proxy_port,n);
}
else if(n == 0) {
break;
}
else if(n == SOCKET_ERROR){
errorno = WSAGetLastError();
printf("client socket closed or error:%d\n",errorno);
break;
}
// }
// FD_ZERO(&fdwrite);
// FD_SET(cli_socket, &fdwrite);
// ret = select(NULL, NULL, &fdwrite, NULL, NULL);
// if(ret == SOCKET_ERROR)
// {
// printf("select error!%d\n",WSAGetLastError());
// closesocket(cli_socket);
// ExitThread(0);
// }
// if(FD_ISSET(cli_socket, &fdwrite)) {
/* 返回数据给客户端 */
n = send(cli_socket,msg,n,0);
if( n > 0){
// printf("[%d]:To Client ----> [%d]\n",GetCurrentThreadId(),n);
printf("[%d]:C[%-16s:%-5d] << S[%-16s:%-5d] [%d]\n",
GetCurrentThreadId(),ClientIP.ip,ClientIP.port,server_addr,server_port,n);
}
else if(n == SOCKET_ERROR){
errorno = WSAGetLastError();
printf("client socket closed or error:%d\n",errorno);
break;
}
}
}
closesocket(pro_socket);
closesocket(cli_socket);
thread_no --;
printf("exit thread [%d]....\n",GetCurrentThreadId());
return 1;
}
/*****************************************************************/
/* NAME : ListenThread */
/* IN : LPVOID lpParam */
/* OUT : DWORD */
/* Sumary: 处理客户请求的线程 */
/*****************************************************************/
DWORD WINAPI ListenThread(LPVOID lpParam)
{
int addrlen,errorno;
HANDLE hThread[MAX_THREAD];
SOCKET client; /* 客户端SOCKET */
struct sockaddr_in cli_addr; /* 客户端的地址 */
tClientIP ClientIP;
addrlen = sizeof(cli_addr);
while(1)
{
ZeroMemory(&cli_addr,sizeof(cli_addr));
/* 等待接收客户连接请求 */
client = accept(hostserver,(struct sockaddr*)&cli_addr,&addrlen);
/* 获取客户端临时端口 */
strcpy(ClientIP.ip , inet_ntoa(cli_addr.sin_addr));
ClientIP.port = cli_addr.sin_port;
ClientIP.cli_socket = client;
/* 开启服务线程 */
hThread[thread_no] = CreateThread(
NULL, //缺省的安全性
0, //缺省的堆栈
ServerThread, //线程入口
reinterpret_cast<lpvoid>(&ClientIP), //客户端socket
0, //无特殊创建标志
&thID); //返回线程的ID
if(hThread[thread_no] == NULL){
errorno = WSAGetLastError();
printf("thread create error! %d\n",errorno);
closesocket(ClientIP.cli_socket);
}
else{
thread_no++;
}
}
}
/****************************************************************/
/* NAME : main */
/* IN : NULL */
/* OUT : NULL */
/* Sumary: 入口程序(创建监听线程) */
/****************************************************************/
void main()
{
HANDLE hListen;
int errorno;
/* 初始化服务器 */
if(InitServer() < 0){
/* 初始化失败 */
errorno = GetLastError();
printf("Init error:%d",errorno);
ExitProcess(0);
}
/* 创建监听线程 */
hListen = CreateThread(
NULL, //缺省的安全性
0, //缺省的堆栈
ListenThread, //监听线程入口
NULL, //
0, //无特殊创建标志
&thID); //返回线程的ID
if(hListen = NULL)
{
/* 创建线程失败 */
errorno = WSAGetLastError();
printf("thread create error! %d\n",errorno);
WSACleanup();
ExitProcess(0);
}
// ::WaitForSingleObject(hListen,INFINITE);
Sleep(999999);
closesocket(hostserver);
WSACleanup();
}
////////////////////////////////////////////////////////////////////////////
//lots of problems , can u ?
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
Hi!
I have installed libpcap to my linux computer, and I have made a simple program that uses the file pcap.h in /include.
I having problem with looking up the device. My code is:
dev = pcap_lookupdev(errbuf);
and message I get when I compile is
/home/steve/tmp/ccCzGEwY.o(.text+0x1e): In function `main':
: undefined reference to `pcap_lookupdev'
collect2: ld returned 1 exit status
Can someone help me??
Thanks for your time
/Stefan
|
|
|
|
|
This just means that the function 'pcap_lookupdev' is not defined properly. Either it is missing from the header file or you don't have the actual implementation of the function.
Did you remember to install any accompanied library files as well ? Remembered to include those libraries to the build/compile phase ?
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
You need to add -lpcap to your compile options, and ensure that libpcap.o is in a place that can be found by the compiler (either in the path specified by the LIB environment variable, or add the path using the -L option, IIRC).
|
|
|
|
|
what would be appropriate mechanism to write into Edit boxes of another application.
|
|
|
|
|
You could put your text into the clipboard and then send the Editbox a WM_PASTE-message.
This approach needs some research about the format of text in the clipboard and the exact content of the WM_PASTE-message but you can maybe look at some of the special editboxes here at CP some of which handle WM_PASTE.
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
by default a floating toolbar that is not docked has the red 'x' at the top-right corner to allow users to close the toolbar. does anyone know how to disable/remove that 'x' so the toolbar will always exist in the client area of an MDI app? thanks for any help. take care.
|
|
|
|
|
This offsite article should help Remove close button from floating toolbar[^]
Roger Allen - Sonork 100.10016
Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...
|
|
|
|
|
thanks. i implemented the solution and it works just fine.
|
|
|
|
|
Hi All
My idea was to perform autologin once i boot my computer.
So i wrote a windows Service that does the following
1. Wait for 5 minutes.
2. Simulate password characters and the ENTER key so that the login happens.
(using the keybd_event API)
The keybd_event executed from a windows Service does no seem to work.
The service type has the flag SERVICE_INTERACTIVE_PROCESS. But still it does not work.
If i login manually before 5 minutes and open a notepad, then the passowrd and ENTER is automatically typed after 5 minutes of booting.
BUT THE KEYBD_EVENT DOES NOT AFFECT THE LOGIN SCREEN AT ALL.
What is the correct approach for acheiving this functionality >
Thanx
Kiran
|
|
|
|
|
|
Thanx for this useful info.
However I want to do this programatically using some key simulation as i wuld need a similar functionality when the user has locked the workstation.
Is there no way we can simulate keystrokes to msgina ??
Thanx
Kiran
|
|
|
|
|
hi,
i need to make http request to server through web proxy servers. any one help to me. thanks in advance.
Murali.M
|
|
|
|
|
i think you send the same request to the proxy than to the server.
the only difference is that the url is not just the path, you have to send the full url.
Don't try it, just do it!
|
|
|
|
|
Hi All,
I want to write my own plugin for internet explorer,since i have not this ,from where and how to start any resource.
Thanks in advance,
AL
|
|
|
|
|
|
I want to write user name and password in login window of Yahoo messenger from my application.
Till now I am able to get Handle to Login window using Getclass and GetwindowText.My plan is to get the handle of Login window , then that of edit boxes of Login window. Then to paste text into edit boxes.
The problem is how to know whether this login (which is a dialog box) or edit boxes belongs to Yahoo messenger or a particular window.
|
|
|
|
|
presumably you have used the findwindow function to get the handle for the llogin window
fromthere you can find the client windows by using findwindowex
use spy ++ to gather information about the login window
is this what you have been trying?
bryce
---
Publitor, making Pubmed easy.
http://www.sohocode.com/publitor
|
|
|
|
|
<presumably you="" have="" used="" the="" findwindow="" function="" to="" get="" handle="" for="" login="" window.="">
GetWindowText is giving me handle. But handle is generated at runtime. It is not unique.
Yes U are very much near to my goal.
Cheers!!!!!!!
Siddharth
|
|
|
|
|
The Login window of Yahoo messenger is a dialog resource
hence the classname is #32770 (dialog) which is not unique
but the Parent of this login window is "YahooBuddyMain" (classname) , you can check the parent of the dialog via
GetParent and then see whether it is a Yahoo login window or not
Better Yet try to get the GetDlgCtrlID of the login window that should be consistant
But beware there can be no sure fire way of doing what you are trying
Hope that helps
Live as if your were to die tomorrow. Learn as if you were to live forever.
-Mahatma Gandhi
|
|
|
|
|
That's exactly what I need.
But is there any other better method of doing it. More generalized.
Thanx
|
|
|
|
|
Hey everyone!
I am working on this freeware application that does archiving of certain files. I am wondering can anyone point me in the best direction on how to implement ZIP from within my own code? (Preferably a FREE method). I've found a company that allows you to buy and redistribute a .DLL from them that you can call methods to ZIP, but they want to charge for the amount of distributions. And since its FREEWARE, I don't really want to pay for everyone that uses this program freely!
In the meantime, what I've done is installed WinZip's command line addon, and I've been calling that with a CreateProcess() command. But I don't want to have to my users have to download and install WinZip, so that my program works. I want everything in one bundle preferably to call some code or a (FREE) .DLL within my own app that creates the .ZIP file!
Thanks
|
|
|
|
|
|
First off, I am a newbe. I have learned C++ from book on my own because I seek knowledge and I like to know as much stuff as I can. I love to program.
Using a MFC app. You can place a static or edit control on the dialog and declare a Cstring to it. but as far as I can manage you cant get it to display both numbers and letters. How do I make a edit or static control display something like “gun + 12” (space ship game I am making).
.format
did not work
“I do the best possible thing in the worst possible way”
rea
|
|
|
|
|
Do you mean you are trying to set the text of your control?
Try
GetDlgItem([your control ID])->SetWindowText("gun + 12")
If you want to put a variable value into it, try:
CString display;
display.Format("gun + %d", your_variable);
GetDlgItem([your control ID])->SetWindowText(display);
Hope this helps
|
|
|
|
|