Thread: Network Program Not Connecting

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Network Program Not Connecting

    Can someone please explain to me what I'm doing wrong... From what I can see, there is nothing wrong with my code. The server is supposed to send me 3 integers as a response, but the problem is it never even connects, the call to connect prints "connect error" every time... Thanks:

    Code:
    #include <iostream>
    #include <Winsock2.h>
    #define ADDRESS "69.55.233.82"
    #define PORT 5842
    
    using namespace std;
    
    int main() {
    	
    	WSADATA wsaData;
    	sockaddr_in serv;
    	SOCKET sock;
    
    	if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
    		printf("WSAStartup error");
    		exit(1);
    	}
    	
    	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	serv.sin_family = AF_INET;
    	serv.sin_addr.S_un.s_addr = inet_addr(ADDRESS);
    	serv.sin_port = htons(PORT);
    
    	if (connect(sock,(sockaddr *)&serv, sizeof(serv)) == SOCKET_ERROR) {
    		printf("connect error");
    		WSACleanup();
    		exit(1);
    	}
    	int a = 0, b = 0, c = 0;
    	recv(sock, (char *)&a, sizeof(unsigned int), 0);
    	recv(sock, (char *)&b, sizeof(unsigned int), 0);
    	recv(sock, (char *)&c, sizeof(unsigned int), 0);
    	printf("int a: %d\nint b: %d\nint c: %d", a, b, c);
    	closesocket(sock);
    	WSACleanup();
    
    }
    Last edited by pobri19; 12-23-2008 at 03:57 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by http://msdn.microsoft.com/en-us/library/ms737625(VS.85).aspx
    Return Value

    If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
    What does WSAGetLastError say?

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    It's all good, turns out the server was having difficulties and the code does in fact work. Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. How to start program P2P network?
    By gogo in forum C Programming
    Replies: 2
    Last Post: 07-27-2004, 07:35 AM
  4. network program
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 10:47 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM