lichongbin
July 8th, 2007, 11:02 AM
How to establish connection between two computers through internet, not LAN?
|
Click to See Complete Forum and Search --> : throught internet! lichongbin July 8th, 2007, 11:02 AM How to establish connection between two computers through internet, not LAN? S_M_A July 8th, 2007, 11:06 AM What kind of connection? wildfrog July 8th, 2007, 11:08 AM There is really no difference. The two PC's doesn't know whether it's a LAN, WAN, the Internet etc. You can use sockets. - petter JVene July 8th, 2007, 11:36 AM Wildfrog is certainly right, just open any sockets sample app or FTP sample app and do what they've done to open a socket. However, there are just a few things to consider. When you say "between two computers" - there can be a minor catch. If the other computer is a server, like a hosting service FTP server, you'll have no difficulty at all as long as you can otherwise browse. If, however, you own both computers (or, let's say your application is running on two client Windows machines with dial-up or DSL), there can be firewalls, routers or other blockage in the way. Put another way, at least one of the computers involved has to have a public IP address. For a single connection session it doesn't HAVE to be static, just public. For example, which you might already know, if you have a LAN that shares an internet connection, all the computers on the LAN have IP addresses visible to the router, but not to the public. Your router doesn't forward your IP to the world. The only way to get a computer on the LAN to act as a server 'to the world' at large, will be if the router has a public IP address 'facing the world' and will forward your server connection from that IP to the client's IP. Some routers can do that, others can't. It's a minor point, but your question is rather general, so I thought I'd point it out. S_M_A July 8th, 2007, 12:26 PM lichongbin, why I asked what connection you had in mind was because I feared that you thought about sharing a disk or something similar and this is in general not a very good idea. lichongbin July 8th, 2007, 07:19 PM I am sorry that I didn't express my question clearly. Actually, I have written two simple programs which can exchange video data each other on the LAN. They just look like the following code: Client: void main() { // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); if ( iResult != NO_ERROR ) printf("Error at WSAStartup()\n"); // Create a socket. SOCKET m_socket; m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( m_socket == INVALID_SOCKET ) { printf( "Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return; } // Connect to a server. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); clientService.sin_port = htons( 27015 ); if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { printf( "Failed to connect.\n" ); WSACleanup(); return; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Client: Sending data."; char recvbuf[32] = ""; bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, recvbuf, 32, 0 ); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { printf( "Connection Closed.\n"); break; } if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } return; } Server: void main() { // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); if ( iResult != NO_ERROR ) printf("Error at WSAStartup()\n"); // Create a socket. SOCKET m_socket; m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( m_socket == INVALID_SOCKET ) { printf( "Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return; } // Bind the socket. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = htonl(INADDR_ANY); service.sin_port = htons( 27015 ); if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { printf( "bind() failed.\n" ); closesocket(m_socket); return; } // Listen on the socket. if ( listen( m_socket, 1 ) == SOCKET_ERROR ) printf( "Error listening on socket.\n"); // Accept connections. SOCKET AcceptSocket; printf( "Waiting for a client to connect...\n" ); while (1) { AcceptSocket = SOCKET_ERROR; while ( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( m_socket, NULL, NULL ); } printf( "Client Connected.\n"); m_socket = AcceptSocket; break; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Server: Sending Data."; char recvbuf[32] = ""; bytesRecv = recv( m_socket, recvbuf, 32, 0 ); printf( "Bytes Recv: %ld\n", bytesRecv ); bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); return; } However, they doesn't work between two computers which are not on the same LAN. JVene's explaintion helps me a lot. Actually I just don't know how to program exactly? S_M_A July 8th, 2007, 07:31 PM Please add code tags. For the client side you just have to change from 127.0.0.1 to the server ip and as jvene said it has to be the public ip. For the server side you have to change your router settings. Since your server expects the client to connect at port 27015 your router has to be set to forward all accesses to that port to your server. Be aware that by doing this you also make your machine vulnerable to attacks like buffer overrun and so on. Edit: Added picture example of router setup for server JVene July 8th, 2007, 07:42 PM Next time, use code tags - you can try the 'code' icon in the tool bar above the editor window for an example. From a very quick glance at your code, it looks like you're already in the right ballpark. If that works on a LAN, there's little reason it won't work over the 'net, it's just a matter of knowing the IP of at least one of the two computers, and making sure that IP is public. The only way to get two computers to exchange with each other that are behind proxies or firewalls is to use some server that can support the exchange. That's how most of the IM systems work. I doubt that's a solution for you to plan on, but just in case it turns out you can, it's an option. It requires that you run an application on that server which accepts connections from both clients and forwards packets of data from one to the other. You can use an FTP server and client, or a telnet chat application, to test if two computers can 'find' each other. At least one of the computers should not be behind a proxy for the test, or you would have to know if the proxy will forward server ports for you. Assuming one is not behind a proxy (as in 'directly' connected to DSL or something like that), use IPCONFIG in Windows to find the IP address of the computer you intend to be the server. Launch an FTP server on that machine, then use the client to see if you can connect to it. Make sure you understand the server/client software and CAN make it work by testing on your LAN (locally). If they connect that way, you should be able to use your application by position your server application on the computer that successfully functioned as a server in the FTP test. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |