WinSock: Won't connect

This is a discussion on WinSock: Won't connect within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock.h> void main ( void ) { WSADATA WsaDat; if (WSAStartup(MAKEWORD(1, 1), ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    83

    WinSock: Won't connect

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    void main ( void )
    {
    	WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    	SOCKET Socket;
          Socket = socket(AF_INET, SOCK_STREAM, 0);
          if (Socket == INVALID_SOCKET)
               {
              printf("Socket creation failed.");
               }
    
    SOCKADDR_IN SockAddr;
    
    SockAddr.sin_port = 50;
    
    SockAddr.sin_family = AF_INET;
    
    SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
    SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
    SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
    SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;
    
    connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr));
    
    }
    Ok, I have a server set up on port 50 also.

    This will not connect, why?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    83
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    void main ( void )
    {
    	WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    	int Socket;
          Socket = socket(AF_INET, SOCK_STREAM, 0);
          if (Socket == INVALID_SOCKET)
               {
              printf("Socket creation failed.");
               }
    
    SOCKADDR_IN SockAddr;
    
    SockAddr.sin_port = htons(50);
    
    SockAddr.sin_family = AF_INET;
    
    SockAddr.sin_addr = inet_addr( "127.0.0.1" );
    
    	if( connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) < 0 ) {
    	  perror( "connect" );
    	}
    
    
    }
    now it won't compile with this message:
    Error "c:\documents and settings\administrator\desktop\main.c": 27 operands of = have illegal types 'struct in_addr' and
    + 'unsigned long'
    Compilation + link time:0.3 sec, Return code: 1

    Also I did not get what you ment by assigning the structs, if you could please edit that source to show me what you mean, it would be greatly appreciated.
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    83
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    void main ( void )
    {
    	WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    	int Socket;
          Socket = socket(AF_INET, SOCK_STREAM, 0);
          if (Socket == INVALID_SOCKET)
               {
              printf("Socket creation failed.");
               }
    
    SOCKADDR_IN SockAddr;
    
    	if (Socket < 0) {
    		struct sockaddr_in SockAddr;
    
    		SockAddr.sin_port = htons(50);
    		SockAddr.sin_family = AF_INET;
    		SockAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    	}
    
    	if( connect(Socket, (struct sockaddr *)(&SockAddr), sizeof(SockAddr)) < 0 ) {
    	  perror( "connect" );
    	}
    
    }
    that right? It compiles, outputs "Connect: No error."
    but does not connect, also when i shutdown the server it still says "Connect: No error" when it couldn't possibly be connecting.
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    83

    weird...

    Now it works..sorta. Ok when the server is on it returns nothing...which is all good and correct right? But when it is off it returns Connect:No error, which is throwing me off because there was an error; there was no server?
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    83

    Ok so say it is connecting...

    Say it is connecting, and I think it is, but only for a split second because my server doesn't show the connection, but unless the server is on, the program returns the error..anywasy:

    how do I get the information being sent to me, and output it to the screen? This is what im doing, it returns "T":

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    void main ( void )
    {
    	WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    	int Socket;
          Socket = socket(AF_INET, SOCK_STREAM, 0);
          if (Socket == INVALID_SOCKET)
               {
              printf("Socket creation failed.");
               }
    
    struct sockaddr_in SockAddr;
    
    	if (Socket < 0) {
    		struct sockaddr_in SockAddr;
    		perror("socket");
    	}
    
    SockAddr.sin_port = htons(50);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    
    	if( connect(Socket, (struct sockaddr *)(&SockAddr), sizeof(SockAddr)) < 0 ) {
    	  printf( "\nCould not connect to remote server.\n" );
    	}
    
    	else {
    		connect(Socket, (struct sockaddr *)(&SockAddr), sizeof(SockAddr));
    
    		char *re[30];
    		recv(Socket, *re, 30, 0);
    
    		printf("%c", &*re);
    
    		}
    }
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    83
    woah:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    void main ( void )
    {
    	WSADATA WsaDat;
         if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
               {
                printf("WSA Initialization failed.");
               }
    
    	int Socket;
          Socket = socket(AF_INET, SOCK_STREAM, 0);
          if (Socket == INVALID_SOCKET)
               {
              printf("Socket creation failed.");
               }
    
    struct sockaddr_in SockAddr;
    
    	if (Socket < 0) {
    		struct sockaddr_in SockAddr;
    		perror("socket");
    	}
    
    SockAddr.sin_port = htons(50);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    
    	if( connect(Socket, (struct sockaddr *)(&SockAddr), sizeof(SockAddr)) < 0 ) {
    	  printf( "\nCould not connect to remote server.\n" );
    	}
    
    	else {
    		connect(Socket, (struct sockaddr *)(&SockAddr), sizeof(SockAddr));
    
    		int re[100];
    		recv(Socket, re, 100, 0);
    
    		printf("%s", &re);
    
    		}
    }
    this does what i want it to, BUT with a few warning errors, but it compiles, the only thing is, it does not seem to be maintainng the connection.

    Also for any access amount of charecters it does something like "ZZZZ-ZZZZ", how can i make it return only the amount of charecters sent to it by the server?
    "What this country needs is more free speech worth listening to." - -Hansell B. Duckett

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (C++) Winsock connect and listen program. Need Help!
    By azjherben in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-05-2009, 08:44 PM
  2. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  3. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 05:00 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. Winsock working only for 127.0.0.1 Cant connect to other PCs
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-03-2002, 05:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21