socket programming using vc++
-
hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----
#include <windows.h>
#include <winsock.h>
#include <stdio.h>#define NETWORK_ERROR -1
#define NETWORK_OK 0void ReportError(int, const char *);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
struct in_addr addr;
addr.s_addr = inet_addr("127.0.0.1");sockVersion = MAKEWORD(1, 1); // Initialize Winsock as before WSAStartup(sockVersion, &wsaData); // Store information about the server LPHOSTENT hostEntry; hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET); // Specifying the server by its name; // another option: gethostbyaddr() if (!hostEntry) { nret = WSAGetLastError(); ReportError(nret, "gethostbyaddr()"); // Report the error as before WSACleanup(); return NETWORK\_ERROR; } // Create the socket SOCKET theSocket; theSocket = socket(AF\_INET, // Go over TCP/IP SOCK\_STREAM, // This is a stream-oriented socket IPPROTO\_TCP); // Use TCP rather than UDP if (theSocket == INVALID\_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "socket()"); WSACleanup(); return NETWORK\_ERROR; } // Fill a SOCKADDR\_IN struct with address information SOCKADDR\_IN serverInfo; serverInfo.sin\_family = AF\_INET; // At this point, we've successfully retrieved vital information about the server, // including its hostname, aliases, and IP addresses. Wait; how could a single // computer have multiple addresses, and exactly what is the following line doing? // See the explanation below. serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list); serverInfo.sin\_port = htons(80); // Change to network-byte order and // insert into port field // Connect to the server nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); ReportError(nret, "connect()"); WSACleanup(); return NETWORK\_ERROR; } MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK); // Successfully connected! // Send/receive, then cleanup:
-
hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----
#include <windows.h>
#include <winsock.h>
#include <stdio.h>#define NETWORK_ERROR -1
#define NETWORK_OK 0void ReportError(int, const char *);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
struct in_addr addr;
addr.s_addr = inet_addr("127.0.0.1");sockVersion = MAKEWORD(1, 1); // Initialize Winsock as before WSAStartup(sockVersion, &wsaData); // Store information about the server LPHOSTENT hostEntry; hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET); // Specifying the server by its name; // another option: gethostbyaddr() if (!hostEntry) { nret = WSAGetLastError(); ReportError(nret, "gethostbyaddr()"); // Report the error as before WSACleanup(); return NETWORK\_ERROR; } // Create the socket SOCKET theSocket; theSocket = socket(AF\_INET, // Go over TCP/IP SOCK\_STREAM, // This is a stream-oriented socket IPPROTO\_TCP); // Use TCP rather than UDP if (theSocket == INVALID\_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "socket()"); WSACleanup(); return NETWORK\_ERROR; } // Fill a SOCKADDR\_IN struct with address information SOCKADDR\_IN serverInfo; serverInfo.sin\_family = AF\_INET; // At this point, we've successfully retrieved vital information about the server, // including its hostname, aliases, and IP addresses. Wait; how could a single // computer have multiple addresses, and exactly what is the following line doing? // See the explanation below. serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list); serverInfo.sin\_port = htons(80); // Change to network-byte order and // insert into port field // Connect to the server nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); ReportError(nret, "connect()"); WSACleanup(); return NETWORK\_ERROR; } MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK); // Successfully connected! // Send/receive, then cleanup:
not sure I fully understand your issue you've created a tcp socket client app 1) you try to connect to (local loopback address 127.0.0.1) on port 80 and get an error - do you have an ethernet adaptor, and tcp/ip set up on your machine ? - you should be able to ping yourself on 127.0.0.1 and get a valid response before you try this - then you'll need something listening on port 80 - depending on what sort of a machine, it could be IIS for example, or a server harness 2) you try a random port - unless you fix the issues at (1), its not going to work AND you would need something listening on all the ports on machine to catch the connect on a 'random' port - I dont see what a random port does for your case 'g'
-
hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----
#include <windows.h>
#include <winsock.h>
#include <stdio.h>#define NETWORK_ERROR -1
#define NETWORK_OK 0void ReportError(int, const char *);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
struct in_addr addr;
addr.s_addr = inet_addr("127.0.0.1");sockVersion = MAKEWORD(1, 1); // Initialize Winsock as before WSAStartup(sockVersion, &wsaData); // Store information about the server LPHOSTENT hostEntry; hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET); // Specifying the server by its name; // another option: gethostbyaddr() if (!hostEntry) { nret = WSAGetLastError(); ReportError(nret, "gethostbyaddr()"); // Report the error as before WSACleanup(); return NETWORK\_ERROR; } // Create the socket SOCKET theSocket; theSocket = socket(AF\_INET, // Go over TCP/IP SOCK\_STREAM, // This is a stream-oriented socket IPPROTO\_TCP); // Use TCP rather than UDP if (theSocket == INVALID\_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "socket()"); WSACleanup(); return NETWORK\_ERROR; } // Fill a SOCKADDR\_IN struct with address information SOCKADDR\_IN serverInfo; serverInfo.sin\_family = AF\_INET; // At this point, we've successfully retrieved vital information about the server, // including its hostname, aliases, and IP addresses. Wait; how could a single // computer have multiple addresses, and exactly what is the following line doing? // See the explanation below. serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list); serverInfo.sin\_port = htons(80); // Change to network-byte order and // insert into port field // Connect to the server nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); ReportError(nret, "connect()"); WSACleanup(); return NETWORK\_ERROR; } MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK); // Successfully connected! // Send/receive, then cleanup:
-
not sure I fully understand your issue you've created a tcp socket client app 1) you try to connect to (local loopback address 127.0.0.1) on port 80 and get an error - do you have an ethernet adaptor, and tcp/ip set up on your machine ? - you should be able to ping yourself on 127.0.0.1 and get a valid response before you try this - then you'll need something listening on port 80 - depending on what sort of a machine, it could be IIS for example, or a server harness 2) you try a random port - unless you fix the issues at (1), its not going to work AND you would need something listening on all the ports on machine to catch the connect on a 'random' port - I dont see what a random port does for your case 'g'
thanks for the reply. i can ping 127.0.0.1 easily and getting a valid output.but i dont know how to ping using a port. i have 2 ethernet adaptors, one is used for internet and other is spare. my internet is working find, so port 80 is also working well ??
-
thanks for the reply. i can ping 127.0.0.1 easily and getting a valid output.but i dont know how to ping using a port. i have 2 ethernet adaptors, one is used for internet and other is spare. my internet is working find, so port 80 is also working well ??
rahuljin wrote:
so port 80 is also working well ??
not necessarily - you'll likely be connecting out to a web-server on port 80 on that server - unless you have something listening on your own machine on port 80 you'll get a connect fail 'g'
-
'Connection refused' means that port is not active (no TCP server using the port number). 'Network is unreachable' might be meant some firewall had blocked the packet. Can you check the firewall settings?
-
rahuljin wrote:
so port 80 is also working well ??
not necessarily - you'll likely be connecting out to a web-server on port 80 on that server - unless you have something listening on your own machine on port 80 you'll get a connect fail 'g'
-
rahuljin wrote:
can u tell me the changes i should make so that it can work ?
There are no changes to make in your code. I just ran it and it works as expected.
which port are you using ? if i use a port which is used by other program like utorrent, it works only when i run utorrent, else dont. if i put some other port number, it shows the errors. i am using kaspersky internet security 2009 with windows 7rc and visual studio 2008. windows firewall is stopped. i also checked with KIS stopped, but no success. now what to do ?
-
sorry, o/night here in Aus... as per Led Mike, there's nothing wrong with your code. Now you need to develop a server or grab some test code from someone's project to build one. TCP programming involves two parts - a client, which you've got, and server, listening on a port - you still havnt indicated that you have a server listening to/on a connection, so Im not sure what sorta results you expect. 'g'
-
sorry, o/night here in Aus... as per Led Mike, there's nothing wrong with your code. Now you need to develop a server or grab some test code from someone's project to build one. TCP programming involves two parts - a client, which you've got, and server, listening on a port - you still havnt indicated that you have a server listening to/on a connection, so Im not sure what sorta results you expect. 'g'
-
do u mean that i should write another program which listen to this port ?? also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?
rahuljin wrote:
do u mean that i should write another program which listen to this port ??
yes - the whole point of connecting to something is something must be waiting/listening on the other end ! .. maybe you could use something like http://www.aprelium.com/abyssws/[^]
rahuljin wrote:
also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?
if you are in the connected state, you have a channel open and you can send to and receive from it - depending on whats on the other end - the simplest test of your code is whats known as an echo server - it reads what you send it and sends you back the same info - see here for an example - I dont get why you're asking about another port http://www.paulgriffiths.net/program/c/echoserv.php[^] and here http://www.csc.villanova.edu/~mdamian/Sockets/TcpSockets.htm[^] I hate to say this, but it sounds like you're a little out of your depth - this sort of material is covered in lots of network programming books, and there's plenty out there on the net Maybe you should spent some time doing a bit more research
-
rahuljin wrote:
do u mean that i should write another program which listen to this port ??
yes - the whole point of connecting to something is something must be waiting/listening on the other end ! .. maybe you could use something like http://www.aprelium.com/abyssws/[^]
rahuljin wrote:
also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?
if you are in the connected state, you have a channel open and you can send to and receive from it - depending on whats on the other end - the simplest test of your code is whats known as an echo server - it reads what you send it and sends you back the same info - see here for an example - I dont get why you're asking about another port http://www.paulgriffiths.net/program/c/echoserv.php[^] and here http://www.csc.villanova.edu/~mdamian/Sockets/TcpSockets.htm[^] I hate to say this, but it sounds like you're a little out of your depth - this sort of material is covered in lots of network programming books, and there's plenty out there on the net Maybe you should spent some time doing a bit more research
-
'Connection refused' means that port is not active (no TCP server using the port number). 'Network is unreachable' might be meant some firewall had blocked the packet. Can you check the firewall settings?
-
is there any way to give an address to bind() ?? i want that the program only except or recevice information from a specific address only. but when i try it gives an error - 10049. how should i resolve it ?
-
Post your code to bind() here, pls. And also, the IP address of the pc that you executed the program.
here is the member funtion ----
int getServer::getInfo()
{
ifstream ifile(path);
if(ifile)
{
ifile.getline(sss, 80);
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
listeningSocket = socket
(
AF_INET,
SOCK_STREAM,
IPPROTO_TCP
);if (listeningSocket == INVALID\_SOCKET) { nret = WSAGetLastError(); reportError(nret, "socket()"); WSACleanup(); return NETWORK\_ERROR; } serverInfo.sin\_family = AF\_INET; serverInfo.sin\_addr.s\_addr = inet\_addr(sss); serverInfo.sin\_port = htons(23571); nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "bind()"); WSACleanup(); return NETWORK\_ERROR; } nret = listen(listeningSocket, 10); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "listen()"); WSACleanup(); return NETWORK\_ERROR; } theClient = accept ( listeningSocket, NULL, NULL ); if (theClient == INVALID\_SOCKET) { nret = WSAGetLastError(); reportError(nret, "accept()"); WSACleanup(); return NETWORK\_ERROR; } byteRece = recv(theClient, st, 100, 0); if (byteRece == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "send()"); WSACleanup(); return NETWORK\_ERROR; } MessageBoxA(NULL, st, "Server Status", MB\_OK | MB\_ICONEXCLAMATION); closesocket(theClient); closesocket(listeningSocket); WSACleanup(); return NETWORK\_OK; }
in ip.txt, if i put the ip --- 127.0.0.1 for same pc, it works or if i set the instruction ---
serverInfo.sin_addr.s_addr = INADDR_ANY;
if i put an ip address of other pc in the network, then bind() shows an error -- 10049. the ip is like 192.168.250.201.
-
here is the member funtion ----
int getServer::getInfo()
{
ifstream ifile(path);
if(ifile)
{
ifile.getline(sss, 80);
sockVersion = MAKEWORD(1, 1);
WSAStartup(sockVersion, &wsaData);
listeningSocket = socket
(
AF_INET,
SOCK_STREAM,
IPPROTO_TCP
);if (listeningSocket == INVALID\_SOCKET) { nret = WSAGetLastError(); reportError(nret, "socket()"); WSACleanup(); return NETWORK\_ERROR; } serverInfo.sin\_family = AF\_INET; serverInfo.sin\_addr.s\_addr = inet\_addr(sss); serverInfo.sin\_port = htons(23571); nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "bind()"); WSACleanup(); return NETWORK\_ERROR; } nret = listen(listeningSocket, 10); if (nret == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "listen()"); WSACleanup(); return NETWORK\_ERROR; } theClient = accept ( listeningSocket, NULL, NULL ); if (theClient == INVALID\_SOCKET) { nret = WSAGetLastError(); reportError(nret, "accept()"); WSACleanup(); return NETWORK\_ERROR; } byteRece = recv(theClient, st, 100, 0); if (byteRece == SOCKET\_ERROR) { nret = WSAGetLastError(); reportError(nret, "send()"); WSACleanup(); return NETWORK\_ERROR; } MessageBoxA(NULL, st, "Server Status", MB\_OK | MB\_ICONEXCLAMATION); closesocket(theClient); closesocket(listeningSocket); WSACleanup(); return NETWORK\_OK; }
in ip.txt, if i put the ip --- 127.0.0.1 for same pc, it works or if i set the instruction ---
serverInfo.sin_addr.s_addr = INADDR_ANY;
if i put an ip address of other pc in the network, then bind() shows an error -- 10049. the ip is like 192.168.250.201.
serverInfo.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
...
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));It is common sennse always succeeds above. But,
serverInfo.sin\_addr.s\_addr = inet\_addr("192.168.250.201");
...
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));will succeeds only when this server program is executed on the pc which has IP address '192.168.250.201' exactly. Note that bind() address is not client address but server address. So you cannot choose client by bind() and you should check accept()'ed client address by means. Another situation, you have some local IP address and global IP address 10.0.0.1 by PPPOE, you may not wait for connection on 10.0.0.1 usually, because 10.0.0.1 address is owned by a router which connecting another network. Then you should wait on your local IP address and make change the router settings, for example DMZ or static routing, which 10.0.0.1 in-bound packets can route to your local IP address. In this case, in fact, it is not a programming issue but the network setting issue.
-
serverInfo.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
...
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));It is common sennse always succeeds above. But,
serverInfo.sin\_addr.s\_addr = inet\_addr("192.168.250.201");
...
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));will succeeds only when this server program is executed on the pc which has IP address '192.168.250.201' exactly. Note that bind() address is not client address but server address. So you cannot choose client by bind() and you should check accept()'ed client address by means. Another situation, you have some local IP address and global IP address 10.0.0.1 by PPPOE, you may not wait for connection on 10.0.0.1 usually, because 10.0.0.1 address is owned by a router which connecting another network. Then you should wait on your local IP address and make change the router settings, for example DMZ or static routing, which 10.0.0.1 in-bound packets can route to your local IP address. In this case, in fact, it is not a programming issue but the network setting issue.
-
thanks. can u tell me how to set the accept() function for getting the ip address of client ? i tried but no result.
sockaddr_in clientAddress;
int clientAddressLen = sizeof(sockaddr_in);
SOCKET clientSocket= accept(linteningSocket, (sockaddr*)&clientAddress, &clientAddressLen);
if (INVALID_SOCKET != clientSocket) {
// client succeeded to connect me
printf("client from %s\n", inet_ntoa(clientAddress.sin_addr);
// start conversation to clientSocket
....
}What do your program codes return above
accept
? No result yet?