socket programming using vc++
-
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?