Shouldn't this code work?

This is a discussion on Shouldn't this code work? within the C++ Programming forums, part of the General Programming Boards category; I dunno, but I am almost certain that this code should work. I have checked it about a hundred times ...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    Shouldn't this code work?

    I dunno, but I am almost certain that this code should work. I have checked it about a hundred times against some other source codes i have found on the internet. Shouldnt this be able to recieve and send messages to a client continuously??? Because right now it only can send messages.


    Code:
    #include <winsock2.h>
    #include <iostream>
    #include <stdio.h>
    #include <process.h>
    
    
    int startupServerForListening(unsigned short port);
    
    void shutdownServer(int socket);
    
    DWORD WINAPI Recieve(int nBytes2, int clientSocket, char* inMessage)
    {   
        
    
    
        while (nBytes2 != 1){
        nBytes2 = recv(clientSocket, inMessage, sizeof(inMessage), 0);
       	
        if (nBytes2 == SOCKET_ERROR) {
    	printf("Recv Failed!\n");
    	system("pause");
    	return 0;
    	}
    
    	printf("Message Received : nbytes2\n", inMessage);
    	
    }	
    
        
        
    }
    
    
    int main() {
    	printf("Welcome to Sandips Online Messenger. . . !\n");
    
    	// the socket my server will use for listening
    	int serverSocket;
    
    	// startup our server utilities with my handy function
    	serverSocket = startupServerForListening(7654);
    
    	// check for errors
    	if (serverSocket == -1) {
    		printf("Network Startup Failed!\nProgram Terminating\n");
    		system("pause");
    		return 0;
    	}
    
    	// accept a client
    	int clientSocket;
    	clientSocket = accept(serverSocket, 0, 0);
    
    	// check for errors
    	if (clientSocket == SOCKET_ERROR) {
    		printf("Accept Failed!\n");
    	}
    
    	int nBytes;
    
    	#define MESSAGE_SIZE BUFSIZ
    
    	char inMessage[MESSAGE_SIZE];
    	char outMessage[MESSAGE_SIZE];
    
    
        int nBytes2;  
        
        HANDLE hThrds[5];
        DWORD aThreadID;
        int i,j;
        DWORD ThreadID;
        
        
                 hThrds[i] = (HANDLE) _beginthreadex(0,0,
    
                (unsigned (_stdcall *)(void *)) Recieve,
    
                (LPVOID) i,0,  (unsigned *) &aThreadID);
    			
    
        
    
        
    	while(outMessage != "/end")
    {
        printf("Message Sent : ", outMessage);
        std::cin.getline (outMessage, MESSAGE_SIZE);
        
    	nBytes = send(clientSocket, outMessage, sizeof(outMessage), 0);
    
    	// check for errors
    	if (nBytes == SOCKET_ERROR) {
    		printf("Send Failed!\n");
    		system("pause");
    		return 0;
    	} 
           else {
    		
    
    	}
    }
    	
    	closesocket(clientSocket);
    
    	shutdownServer(serverSocket);
    
    	system("PAUSE");
    	return 0;
    }
    
    // -----------------------------------------------------------------------------------
    // startupServerForListening() - a function to startup winsock, and open a socket for listening
    
    int startupServerForListening(unsigned short port) {
    
    	// the winsock data structure
    	WSAData wsaData;
    
    	// startup winsock
    	if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR) {
    		printf("Could Not Start Up Winsock!\n");
    		return -1;
    	}
    
    	// create my socket
    	int mySocket = socket(AF_INET, SOCK_STREAM, 0);
    
    	// make sure nothing bad happened
    	if (mySocket == SOCKET_ERROR) {
    		printf("Error Opening Socket!\n");
    		return -1;
    	}
    
    	// the address structure
    	struct sockaddr_in server;
    
    	// fill the address structure with appropriate data
    	server.sin_family = AF_INET;
    	server.sin_port = htons(port);
    	server.sin_addr.s_addr = INADDR_ANY;
    
    	// and now bind my socket
    	if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
    		printf("Bind Failed!\n");
    		closesocket(mySocket);
    		return -1;
    	}
    
    	// mark my socket for listening
    	if (listen(mySocket, 5) == SOCKET_ERROR) {
    		printf("Listen Failed!\n");
    		closesocket(mySocket);
    		return -1;
    	}
    
    	printf("Server Started\n");
    	printf("Waiting for client . . . \n");
    	//_endthread();
    
    	return mySocket;
    }
    
    // -----------------------------------------------------------------------------------
    // shutdownServer() - a function to shutdown a socket and clean up winsock
    
    void shutdownServer(int socket) {
    	// close our socket
    	closesocket(socket);
    
    	// shut down winsock
    	WSACleanup();
    
    	printf("Server Shutdown\n");
    }
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    First this is for network programming!
    Second have a look at:
    Receive multiple data- client- server

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Sorry about that and thanks for the link!
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>inMessage, sizeof(inMessage),
    sizeof(inMessage) == 4 in most cases, since inMessage is a pointer.

    Also, watch out for what you're doing.. you're using std::cin.getline() which blocks, while in your recv() thread you're using printf() to print to the screen. It looks dangerous. You should really really look into CRITICAL_SECTION's and the other synchronization stuff. Also look into _kbhit() or kbhit() so that you won't block with cin.getline().
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 12:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM

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