echo server

This is a discussion on echo server within the Windows Programming forums, part of the Platform Specific Boards category; Does anyone have a source for a simple echo server? I'm trying to learn to use windows sockets. I've followed ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    21

    echo server

    Does anyone have a source for a simple echo server?

    I'm trying to learn to use windows sockets.
    I've followed some tuts but some don't compile and others just do not work.

    http://www.hal-pc.org/~johnnie2/winsock.html
    I can get that to compile and link w/o problems but when i try to send() or recv() i start getting errors
    Last edited by anobody; 10-07-2002 at 06:39 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>when i try to send() or recv() i start getting errors
    Post a snippet of your code where you think the problem is, along with the error messages, and someone will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    21
    I took this stright out of the link above.
    also added a bit more output.

    Code:
    #include <winsock.h>		// winsock.h always needs to be included
    #include <stdio.h>
    
    #define MY_PORT 10000
    #define MY_CONNECTIONS 5
    
    int main(int argc, char** argv) {
    	WORD version = MAKEWORD(1,1);
    	WSADATA wsaData;
    	int nRet;
    
    
    	//
    	// First, we start up Winsock
    	//
    	WSAStartup(version, &wsaData);
    
    
    	//
    	// Next, create the socket itself
    	//
    	SOCKET listeningSocket;
    
    	listeningSocket = socket(AF_INET,		// Go over TCP/IP
    						SOCK_STREAM,		// Socket type
    						IPPROTO_TCP);		// Protocol
    	if (listeningSocket == INVALID_SOCKET) {
    		printf("Error at socket()");
    		WSACleanup();
    		return 0;
    	}
    	
    	printf("Socket created\n");
    	
    	//
    	// Use SOCKADDR_IN to fill in address information
    	//
    	SOCKADDR_IN saServer;
    	
    	saServer.sin_family = AF_INET;
    	saServer.sin_addr.s_addr = INADDR_ANY;		// Since this is a server, any address will do
    	saServer.sin_port = htons(MY_PORT);			// Convert int 8888 to a value for the port field
    	
    	printf("Port: %i\n", MY_PORT);
    
    	//
    	// Bind the socket to our local server address
    	//
            nRet = bind(listeningSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr));
    	if (nRet == SOCKET_ERROR) {
    		printf("Error at bind()");
    		WSACleanup();
    		return 0;
    	}
    
    
    	//
    	// Make the socket listen
    	//
    	printf("Setting socket to lissen\n");
    	
    	nRet = listen(listeningSocket, MY_CONNECTIONS);		// 10 is the number of clients that can be queued
    	if (nRet == SOCKET_ERROR) {
    		printf("Error at listen()");
    		WSACleanup();
    		return 0;
    	}
    	
    	
    	//
    	// Wait for a client
    	//
    	SOCKET theClient;
    
    	printf("Waiting for connections....\nWARNING: Program has been haulted while it waits for connections\n");
    	
    	theClient = accept(listeningSocket,
    			   NULL,			// Address of a sockaddr structure (see below)
    			   NULL);			// Address of a variable containing the size of sockaddr
    	if (theClient == INVALID_SOCKET) {
    		printf("Error at accept()");
    		WSACleanup();
    		return 0;
    	}
    
    
    	// Send/receive from the client, and finally:
    	
    	char myBuf[256];
    
    	nRet = recv(listeningSocket,		// Connected socket
    			    myBuf,		// Receive buffer
    			    sizeof(myBuf),	// Size of the buffer
    			    0);			// More flags
    	if (nRet == SOCKET_ERROR) {
    		printf("Error on recv()\n");
    		//return 0;
    	}
    	
    
    	strcpy(myBuf, "The middle kid is always screwed up.");
    	
    	nRet = send(listeningSocket,		// Pretend this is connected
    				myBuf,				// Our string buffer
    				strlen(myBuf),		// Length of the data in the buffer
    				0);					// Most often is 0, but see end of tutorial for options
    	
    	if (nRet == SOCKET_ERROR) {
    		printf("Error on send()\n");
    		//return 0;
    	}
    
    	printf("Socket clean up\n");
    	closesocket(theClient);
    	closesocket(listeningSocket);
    
    
    	//
    	// Shutdown Winsock
    	//
    	WSACleanup();
    	
    	printf("Returning from main\nProgram ends here\n");
    	return 0;
    }
    Code:
    output:
    Socket created
    Port: 10000
    Setting socket to lissen
    Waiting for connections....
    WARNING: Program has been haulted while it waits for connections
    Error on recv()
    Error on send()
    Socket clean up
    Returning from main
    Program ends here
    Press any key to continue

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Simple problem...

    You're recv()'ing and send()'ing on the wrong socket. You are using the listener, when you should be using the newly created client socket as returned by accept().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    21

    Question

    opps.
    thx

    **turns red**

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. TCP/IP client & echo server
    By Jordban in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2005, 07:39 PM
  3. A breakthrough
    By loopy in forum Linux Programming
    Replies: 4
    Last Post: 11-26-2003, 06:46 PM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM
  5. What's wrong with my Multi-Thread Echo Server ?
    By toshiyuki555_99 in forum C Programming
    Replies: 1
    Last Post: 12-10-2001, 04:52 AM

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