Discussion Locked

Socket programming VC++6

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
I am very new to VC++ and I happen to get some tough work!!!

Can you please give me the code(using VC++6) on how to

1) "Talk" to a particular PORT using a specified IP address.
2) If the return code is -1 then it should print an error else
print success.

Please do help me out on this guys! my neck is on the board!!!

Many Thanks,
John

1 reply

by chris.lewis

Try this link:

http://msdn.microsoft.com/library/en-us/vcsample98/html/vcsmpsimple2.asp

Your basic steps are:
//From the platform SDK code...

#define DEFAULT_PORT "5001" // Default listening port
#define DEFAULT_PROTO SOCK_STREAM // Default to TCP protocol
#define DEFAULT_BUFFER_LEN 4096 // Default recv buffer length

// Load Winsock
if ((retval = WSAStartup(0x202,&wsaData)) != 0)
{
fprintf(stderr,"WSAStartup failed with error %d\n",retval);
WSACleanup();
return -1;
}

..

// Create the socket according to the parameters returned
server_sockets[socket_count] = socket(
addrptr->ai_family,
addrptr->ai_socktype,
addrptr->ai_protocol
);
if (server_sockets[socket_count] == INVALID_SOCKET)
{
fprintf(stderr, "socket failed: %d\n", WSAGetLastError());
goto cleanup;
}

// Bind the socket to the address returned
retval = bind(
server_sockets[socket_count],
addrptr->ai_addr,
addrptr->ai_addrlen
);

// If a TCP socket, call listen on it
if (addrptr->ai_socktype == SOCK_STREAM)
{
retval = listen(
server_sockets[socket_count],
7
);
if (retval == SOCKET_ERROR)
{
fprintf(stderr, "listen failed: %d\n", WSAGetLastError());
goto cleanup;
}
}

//And then you create a thread to handle the listen

server_threads[i] = CreateThread(
NULL,
0,
ServerThread,
(LPVOID)server_sockets[i],
0,
NULL
);
if (server_threads[i] == NULL)
{
fprintf(stderr, "CreateThread failed: %d\n", GetLastError());
goto cleanup;
}

This discussion is closed to new replies.