Thread: cant connect to my server

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    cant connect to my server

    For some reason, I can't seem to connect. I'm using Winsock.

    This is my client program:
    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #pragma lib "ws2_32.lib"
    
    int main()
    {
    	char recvstuff[100];
    	char sendstuff[100];
    	char ip[100];
    
    	printf("type friend's ip address: ");
    	fgets(ip, 25, stdin);
    
    
    	// intialise winsock
    
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    	{
    		printf("error one");
    		getchar();
    		exit(1);
    	}
    
    
    	// getaddrinfo call
    
    	struct addrinfo *result = NULL, *ptr = NULL, hints;
    
    	ZeroMemory(&hints, sizeof (hints));
    	hints.ai_family=AF_UNSPEC;
    	hints.ai_socktype=SOCK_STREAM;
    	hints.ai_protocol=IPPROTO_TCP;
    	
    	if (getaddrinfo(ip, "7772", &hints, &result) != 0)
    	{
    		printf("error two");
    		getchar();
    		exit(1);
    	}
    
    	int sock;
    	
    	// make a socket
    	ptr=result;
    
    	sock=socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    
    	if (sock == -1)
    	{
    		printf("error three");
    		getchar();
    		exit(1);
    	}
    
    	// connect to a server
    	if (connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen) == -1)
    	{
    		printf("error four");
    		getchar();
    		exit(1);
    	}
    
    	printf("connected\n\n\n\n");
    
    	freeaddrinfo(result);
    
    	// main send and recieve loop
    
    	while (strcmp(fgets(sendstuff, 102, stdin), "exit\n") != 0)
    	{
    
    		while (strcspn(sendstuff, "\n") == 102)
    		{
    			fgets(sendstuff, 102, stdin);
    			if (send(sock, sendstuff, sizeof (sendstuff), 0) == -1)
    			{
    				printf("error five");
    				getchar();
    				exit(1);
    			}
    		}
    
    		if (send(sock, sendstuff, sizeof (sendstuff), 0) == -1)
    		{
    			printf("error six");
    			getchar();
    			exit(1);
    		}
    
    		if (recv(sock, recvstuff, 100, 0) == -1)
    		{
    			printf("error seven");
    			getchar();
    			exit(1);
    		}
    		printf("%s\n", recvstuff);
    	}
    
    	// shut down winsock and end program
    
    	closesocket(sock);
    
    	WSACleanup();
    
    	return 0;
    }
    and this is the server program:

    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #pragma lib "ws2_32.lib"
    
    int main()
    {
    	char recvstuff[100];
    	char sendstuff[100]="hi daniel!";
    
    	// intialise winsock
    
    	WSADATA wsaData;
    	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
    	{
    		printf("WSAStartup failed\n\n");
    		exit(1);
    	}
    
    
    	// getaddrinfo call
    
    	struct addrinfo *result=NULL, hints;
    
    	ZeroMemory(&hints, sizeof (hints));
    	hints.ai_family=AF_INET;
    	hints.ai_socktype=SOCK_STREAM;
    	hints.ai_protocol=IPPROTO_TCP;
    	hints.ai_flags=AI_PASSIVE;
    
    	if (getaddrinfo(NULL, "7772", &hints, &result) != 0)
    	{
    		printf("getaddrinfo failed\n\n");
    		exit(1);
    	}
    
    
    
    	// make a socket
    
    	int sock=socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    
    	if (sock == -1)
    	{
    		printf("socket failed\n\n");
    		exit(1);
    	}
    
    
    
    
    	// bind the socket
    
    	if (bind(sock, result->ai_addr, result->ai_addrlen) == -1)
    	{
    		printf("bind failed\n\n");
    		exit(1);
    	}
    
    	freeaddrinfo(result);
    
    
    
    	// listen on the socket
    
    	if (listen(sock, SOMAXCONN) == -1)
    	{
    		printf("listen failed\n\n");
    		exit(1);
    	}
    
    
    
    	// wait for connection
    
    	int mainsock;
    
    	mainsock=accept(sock, NULL, NULL);
    
    	if (mainsock == -1)
    	{
    		printf("accept failed");
    		exit(1);
    	}
    
    	printf("connected\n\n\n\n");
    
    	closesocket(sock);
    
    	while (recv(mainsock, recvstuff, 100, 0) != -1)
    	{
    
    		if (send(mainsock, sendstuff, sizeof (sendstuff), 0) == -1)
    		{
    			printf("send failed\n\n");
    			exit(1);
    		}
    
    		printf("%s", recvstuff);
    	}
    
    	printf("they exited");
    	getchar();
    
    	closesocket(mainsock);
    	WSACleanup();
    	return 0;
    }
    run the server before the client, and when you run the client, type the ip of the server then just type like an IMing service

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So when you say "doesn't work", you mean that nothing at all gets printed on either end?

    Or do you actually see some output / error messages, and are just being vague about it?

    This for debugging, to see what is really happening -> Wireshark · Go deep.
    This for education -> Beej's Guide to Network Programming

    > fgets(sendstuff, 102, stdin);
    If you LIE to fgets() about the size of your buffer, then your program is broken.

    Also, all the send() things should be using say strlen() and not sizeof(). You only want to send "hello\n" for example, not another 95 characters of rubbish along with it.

    Likewise, all your recv() calls need to be tighter as well - use the return result. Also on the recv() side, do NOT attempt to use %s format "as is", because the buffer is NOT automatically appended with a \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    By saying "not working", I mean it doesn't even connect to the server. The client gives me an error four. And thanks about strlen() instead of sizeof, But with recv(), how would we know what the size is? We would have to recieve the information before we know how big it is, right? I also meant to lie to fgets() I did this so that there would be no limit on how big the package is. If it is larger than 100 bytes, it loops back until it has sent all the information. I learned from beej, but he has Linux, so some things are different. But I did learn from him.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But with recv(), how would we know what the size is?
    You tell recv() how big the buffer is.
    recv() returns how many bytes were actually used - this is the number you use as well.

    > I also meant to lie to fgets() I did this so that there would be no limit on how big the package is.
    No, you just broke your code.
    Simply lying about the buffer size doesn't magically turn it into "any size".

    Use a fixed sized buffer, and look for the \n at the end. If there is none, then it's a "long line".
    Write the code to deal with it on that basis.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Okay, I changed the fgets() thing, but I still don't get the recv(). It sounds like you want me to use the return value for one of the arguments. Is this right?

    And, this is kind of off subject.

    for some reason, i cant edit my first post, but lines 74-79 in client are as follows:
    Code:
    	while (strcmp(fgets(sendstuff, 100, stdin), "exit\n") != 0)
    	{
    
    		while (strcspn(sendstuff, "\n") == 100)
    		{
    			fgets(sendstuff, 100, stdin);
    Last edited by pyzaist; 01-26-2010 at 04:52 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If it is a long line, you presently throw away the first part of if before doing anything.

    > while (strcmp(fgets(sendstuff, 100, stdin), "exit\n") != 0)
    Too much in one line.
    Besides which, if fgets() returns NULL (as it would at end of file), it's "hello, what's a segfault?".

    I can't see why the newline problem is even an issue. At best you're just stacking up a problem of what to do with all the line fragments.
    Code:
    while ( fgets(sendstuff, sizeof(sendstuff, stdin) != NULL ) {
        if ( strncmp( sendstuff, "exit", 4 ) == 0 ) break;  // all done
        send( sock, sendstuff, strlen(sendstuff), 0 );  // add retry and error trapping
    }
    This sends every line (or every line fragment) in the right order.

    If all the recv side is doing is processing that (newlines and all), then there is no need for additional complication.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    BTW one kinda good habit to get into with this sort of work (regardless if you are using Winsock or real BSD sockets) is to always send the expected packet length as the first couple of bytes of the message, then follow with the data payload. Reason is that while most of the time the whole message will go thru, that behavior is not guaranteed and you might get your message as a series of smaller packets. Thus if you send the total size as the first few bytes and you don't get that many bytes in the payload you know you have to wait for future packets to complete the message. All that TCP guarantees is that the entire message will get there *eventually* and that the packets will be in-order.

    If you don't keep this in mind you are leaving yourself open to a new and fun class of bugs to troubleshoot...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Thanks guys, but these really are just details right now. This is just a test program. The actual problem is not being able to connect to the server at all. So, it would be nice to not dwell on that problem any more. Not to be rude, but that's not the real problem. Thanks.

    So, why won't it connect to the server? Why does it give me an error four every time?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to connect to a ftp server
    By Coding in forum Networking/Device Communication
    Replies: 6
    Last Post: 07-12-2010, 10:36 AM
  2. Can't get to connect clients to server
    By pyngz in forum C# Programming
    Replies: 2
    Last Post: 03-11-2009, 04:46 AM
  3. Connect to server and get info from mysql db.
    By Sam Granger in forum C++ Programming
    Replies: 2
    Last Post: 01-26-2006, 06:11 AM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

Tags for this Thread