|
-
June 28th, 2008, 01:54 AM
#1
Receive messages in a dialog based application through socket
I am using following code to send message to remote machine through sockets, I am calling mentioned code when a button of MFC dialog based app is clicked and it is working fine. But I don't know how to receive messages from remote machine through sockets.
Can anyone please guide me how to receive messages in dialog based application through socket?
WSADATA w; //Winsock startup info
SOCKADDR_IN target; //Information about host
int error = WSAStartup (0x0202, &w); // Fill in WSA info
if (error)
return 0;
m_sckt = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create socket
if (m_sckt == INVALID_SOCKET)
{
return 0;
}
target.sin_family = AF_INET; // address family Internet
target.sin_port = htons (PortNo); // set server’s port number
target.sin_addr.s_addr = hostname; // set server’s IP
//Try connecting...
if (connect(m_sckt, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR) //Try binding
{ // error
return 0;
}
send(m_sckt, "Test", 5, 0);
Thanks,
Mushq
-
June 28th, 2008, 06:34 AM
#2
Re: Receive messages in a dialog based application through socket
If your remote machine sends messages to your machine, then configure same port on both side(in your dialog based application). make a server client relation between both machine.
And to recieve messages use recv() method.
Code:
int recv( SOCKET s, char* buf, int len, int flags);
Click here for detail: http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|