Click here to Skip to main content
15,888,102 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Ownderdraw and multiple fonts? Pin
Anonymous1-Oct-02 12:19
Anonymous1-Oct-02 12:19 
GeneralRe: Ownderdraw and multiple fonts? Pin
Joaquín M López Muñoz1-Oct-02 12:21
Joaquín M López Muñoz1-Oct-02 12:21 
GeneralRe: Thanks. Ownderdraw and multiple fonts? Pin
Anonymous1-Oct-02 12:35
Anonymous1-Oct-02 12:35 
AnswerRe: Ownderdraw and multiple fonts? Pin
Tomasz Sowinski1-Oct-02 12:13
Tomasz Sowinski1-Oct-02 12:13 
AnswerRe: Ownderdraw and multiple fonts? Pin
Anonymous1-Oct-02 12:15
Anonymous1-Oct-02 12:15 
GeneralRe: Ownderdraw and multiple fonts (2 more questions)? Pin
Anonymous1-Oct-02 12:38
Anonymous1-Oct-02 12:38 
GeneralRe: Ownderdraw and multiple fonts (2 more questions)? Pin
Anonymous2-Oct-02 8:41
Anonymous2-Oct-02 8:41 
GeneralThreaded winsock question Pin
1-Oct-02 12:00
suss1-Oct-02 12:00 
I'm not very familair with C++, but I'm making a program that acts as a client and server.

I've based my code on one of the examples from this web site (http://www.codeproject.com/internet/winsockintro01.asp). Below is the important part of the code:


UINT MTServerThread(LPVOID pParam)
{	
	WSADATA wsaData;
	sockaddr_in local;
	int wsaret=WSAStartup(0x101,&wsaData);

	// WSAStartup returns zero on success
	if(wsaret!=0)
	{
		return 0;
	}

	local.sin_family=AF_INET;
	local.sin_addr.s_addr=INADDR_ANY;
	local.sin_port=htons(listenSendPort);

	server=socket(AF_INET,SOCK_STREAM,0);

	if(server==INVALID_SOCKET)
	{
		return 0;
	}

	if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
	{
		return 0;
	}

	if(listen(server,10)!=0)
	{
		return 0;
	}

	SOCKET client;
	sockaddr_in from;
	int fromlen=sizeof(from);

	while(true)
	{
		client=accept(server,(struct sockaddr*)&from,&fromlen);

		if (client == INVALID_SOCKET)
		{
                        return 0;
		}
		else
		{
			cout << "Connection from " << inet_ntoa(from.sin_addr) <<"\r\n";

			// Spawn a new thread to handle working w/ the client
			AfxBeginThread(ClientThread,(LPVOID)client);	
		}
	}	

	return 0;
}


UINT ClientThread(LPVOID pParam)
{
	char buff[512];
	int n;
	SOCKET client=(SOCKET)pParam;	

	while(true)
	{
		// Read our message into a buffer
		n = recv(client,buff,512,0);
		if(n==SOCKET_ERROR )
		{
			break;
		}
		buff[n]=0;

		// Spit the message out
		cout << "  Received Message: " << buff << "\n";

		break;
	}		

	closesocket(client);
	return 0;
}



My problem is at the end of MTServerThread(), it calls the accept method on the socket. If you notice, I echo out the IP of the connection immediately after.

However, I want my program to be able to have multiple connections going on, and output the message with the persons IP address. Because of this, I need to have access to the persons IP address when I output the received message.

I tried creating a struct containing the client socket and the ip address from MTServerThread, and passed it into ClientThread, but for some reason it wouldn't allow me to cast the struct as LPVOID (which I find odd because I thought SOCKET was just a struct).

I may be wrong, but it looks as if accepting the connection is important to the threading of the program since it only spawns a new thread when a new connection is found, so moving the accept method into the ClientThread, thus allowing me access to the IP there is a no-go.

Any ideas how I can have access to the IP matching the received message in a multi-socket program?
GeneralRe: Threaded winsock question Pin
Joaquín M López Muñoz1-Oct-02 12:06
Joaquín M López Muñoz1-Oct-02 12:06 
GeneralRe: Threaded winsock question Pin
Anonymous1-Oct-02 12:37
Anonymous1-Oct-02 12:37 
GeneralRe: Threaded winsock question Pin
Jon Hulatt1-Oct-02 21:37
Jon Hulatt1-Oct-02 21:37 
GeneralSource Control Integration Pin
Jon Sagara1-Oct-02 11:48
Jon Sagara1-Oct-02 11:48 
GeneralRe: Source Control Integration Pin
Tomasz Sowinski1-Oct-02 11:55
Tomasz Sowinski1-Oct-02 11:55 
GeneralRe: Source Control Integration Pin
Jon Sagara1-Oct-02 12:48
Jon Sagara1-Oct-02 12:48 
GeneralRe: Source Control Integration Pin
Jon Sagara1-Oct-02 14:03
Jon Sagara1-Oct-02 14:03 
GeneralSpecialized User Interface Design for MSN Messenger Pin
James A Beggs1-Oct-02 11:38
James A Beggs1-Oct-02 11:38 
GeneralCFrameWnd::Create Pin
RK_20001-Oct-02 11:26
RK_20001-Oct-02 11:26 
GeneralRe: CFrameWnd::Create Pin
Tomasz Sowinski1-Oct-02 11:39
Tomasz Sowinski1-Oct-02 11:39 
GeneralRe: CFrameWnd::Create Pin
Anonymous1-Oct-02 12:40
Anonymous1-Oct-02 12:40 
GeneralDLL hell. please help! Pin
will13831-Oct-02 10:49
will13831-Oct-02 10:49 
GeneralRe: DLL hell. please help! Pin
Tomasz Sowinski1-Oct-02 11:15
Tomasz Sowinski1-Oct-02 11:15 
GeneralRe: DLL hell. please help! Pin
Dimitri Rochette1-Oct-02 11:44
Dimitri Rochette1-Oct-02 11:44 
Generalproblem with pasting bitmap to the clipboard under win98 Pin
Mariusz Popiolek1-Oct-02 10:49
sussMariusz Popiolek1-Oct-02 10:49 
GeneralRe: problem with pasting bitmap to the clipboard under win98 Pin
Mariusz Popiolek1-Oct-02 11:27
sussMariusz Popiolek1-Oct-02 11:27 
GeneralRe: problem with pasting bitmap to the clipboard under win98 Pin
Tomasz Sowinski1-Oct-02 11:42
Tomasz Sowinski1-Oct-02 11:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.