Winsock Problem!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Winsock Problem!!

  1. #1
    Join Date
    Oct 2017
    Posts
    2

    Winsock Problem!!

    Hello guys, I'm not new to C++ , but recently I jumped over to Network programming and I'm testing some stuff out.
    I compiled both codes Client/Server and everything compiled just fine, but after running , I never get any responde from Client connection .. ??
    thanks in advance.
    Code:
    //server source code
    
    #define WIN32_LEAN_AND_MEAN
    
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    #include <stdio.h>
    #include <iostream>
    #pragma comment(lib, "Ws2_32.lib")
    //GENERAL TYPES
    
    #define DEFAULT_PORT 27015
    #define CLIENTNR 10
    #define CLIENTWAITING 5
    char name[] = {0};
    
    void wsacheck(int data);
    int socketcheck(int data);
    
    int main()
    {
    	int ErrorCheck = NULL;
    
    
    	WSADATA wsa;
    	ErrorCheck = WSAStartup(MAKEWORD(2, 2), &wsa);
    	wsacheck(ErrorCheck);
    	SOCKET srvservice;
    
    	printf("socket()");
    	socketcheck(srvservice = socket(AF_INET, SOCK_STREAM, 0));
    
    	struct sockaddr_in srvaddr;
    
    	srvaddr.sin_family = AF_INET;
        srvaddr.sin_addr.S_un.S_addr = INADDR_ANY;// inet_pton(AF_INET, "192.168.0.103", &srvaddr.sin_addr);
    	srvaddr.sin_port = htons(DEFAULT_PORT);
    
    	
    	if (bind(srvservice, (sockaddr*)&srvaddr, sizeof(srvaddr)) == SOCKET_ERROR)
    	{
    		printf("bind() Failed");
    		closesocket(srvservice);
    	}
    	printf("Listen()");
    	socketcheck(listen(srvservice, CLIENTWAITING));
    
    	SOCKET AcceptSocket;
    	printf("Waiting for a client to connect...\n");
    	while (1) {
    		AcceptSocket = SOCKET_ERROR;
    		while (AcceptSocket == SOCKET_ERROR) {
    			AcceptSocket = accept(srvservice, NULL, NULL);
    		}
    		printf("Client Connected.\n");
    		srvservice = AcceptSocket;
    		break;
    	}
    
    
    	// Send and receive data.
    	int bytesSent;
    	int bytesRecv = SOCKET_ERROR;
    	char sendbuf[32] = "Server: Sending Data.";
    	char recvbuf[32] = "";
    
    	bytesRecv = recv(srvservice, recvbuf, 32, 0);
    	printf("Bytes Recv: %ld\n", bytesRecv);
    
    		bytesSent = send(srvservice, sendbuf, strlen(sendbuf), 0);
    	printf("Bytes Sent: %ld\n", bytesSent);
    
    
    	system("pause");
    	return 0;
    }
    
    
    /////////////////////////Function to Winsock Error checking/////////////////////////
    void wsacheck(int data)
    {
    	if (data != 0)
    	{
    	printf("Initalizing Winsock Error \n");
    	WSACleanup();
    }
    	else
    		printf("Winsock Initializing successful!\n\n");
    }
    
    
    /////////////////////////Function to Socket Error checking/////////////////////////
    int socketcheck(int data)
    {
    	
    	int result = data;
    	if (data == INVALID_SOCKET)
    	{
    		printf(" failed / Error Code: %i \n", WSAGetLastError());
    	}
    	else
    	{
    		printf(" Started successful\n\n");
    	}
    	return result;
    }
    Client
    Code:
    //client source code
    #define WIN32_LEAN_AND_MEAN
    
    #include <winsock2.h>
    #include <Ws2tcpip.h>
    #include <stdio.h>
    #include <iostream>
    #pragma comment(lib, "Ws2_32.lib")
    using namespace std;
    //GENERAL TYPES
    
    #define DEFAULT_PORT 27015
    #define CLIENTNR 10
    #define CLIENTWAITING 5
    char name[] = {0};
    
    void wsacheck(int data);
    int socketcheck(int data);
    
    int main()
    {
    	int ErrorCheck = NULL;
    
    
    	WSADATA wsa;
    	ErrorCheck = WSAStartup(MAKEWORD(2, 2), &wsa);
    	wsacheck(ErrorCheck);
    	SOCKET srvservice;
    
    	printf("socket()");
    	socketcheck(srvservice = socket(AF_INET, SOCK_STREAM, 0));
    
    	struct sockaddr_in srvaddr;
    
    	srvaddr.sin_family = AF_INET;
        srvaddr.sin_addr.S_un.S_addr = INADDR_ANY;// inet_pton(AF_INET, "192.168.0.103", &srvaddr.sin_addr);
    	srvaddr.sin_port = htons(27015);
    
    	
    	if (bind(srvservice, (sockaddr*)&srvaddr, sizeof(srvaddr)) == SOCKET_ERROR)
    	{
    		printf("bind() Failed");
    		closesocket(srvservice);
    	}
    	printf("Listen()");
    	socketcheck(listen(srvservice, CLIENTWAITING));
    
    	SOCKET AcceptSocket;
    	printf("Waiting for a client to connect...\n");
    	while (1) {
    		AcceptSocket = SOCKET_ERROR;
    		while (AcceptSocket == SOCKET_ERROR) {
    			AcceptSocket = accept(srvservice, NULL, NULL);
    		}
    		printf("Client Connected.\n");
    		srvservice = AcceptSocket;
    		break;
    	}
    
    
    	// Send and receive data.
    	int bytesSent;
    	int bytesRecv = SOCKET_ERROR;
    	char sendbuf[32] = "Server: Sending Data.";
    	char recvbuf[32] = "";
    
    	bytesRecv = recv(srvservice, recvbuf, 32, 0);
    	printf("Bytes Recv: %ld\n", bytesRecv);
    
    		bytesSent = send(srvservice, sendbuf, strlen(sendbuf), 0);
    	printf("Bytes Sent: %ld\n", bytesSent);
    
    
    	system("pause");
    	closesocket(srvservice);
    	shutdown(srvservice, 2);
    	return 0;
    }
    
    
    /////////////////////////Function to Winsock Error checking/////////////////////////
    void wsacheck(int data)
    {
    	if (data != 0)
    	{
    	printf("Initalizing Winsock Error \n");
    	WSACleanup();
    }
    	else
    		printf("Winsock Initializing successful!\n\n");
    }
    
    
    /////////////////////////Function to Socket Error checking/////////////////////////
    int socketcheck(int data)
    {
    	
    	int result = data;
    	if (data == INVALID_SOCKET)
    	{
    		printf(" failed / Error Code: %i \n", WSAGetLastError());
    	}
    	else
    	{
    		printf(" Started successful\n\n");
    	}
    	return result;
    }
    Regards

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: Winsock Problem!!

    Code:
    while (AcceptSocket == SOCKET_ERROR)
    If accept() fails, it returns INVALID_SOCKET, not SOCKET_ERROR.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: Winsock Problem!!

    Code:
    SOCKET AcceptSocket;
    	printf("Waiting for a client to connect...\n");
    	while (1) {
    		AcceptSocket = SOCKET_ERROR;
    		while (AcceptSocket == SOCKET_ERROR) {
    			AcceptSocket = accept(srvservice, NULL, NULL);
    		}
    		printf("Client Connected.\n");
    		srvservice = AcceptSocket;
    		break;
    	}
    This seems a bit cumbersome. Why not something like

    Code:
    SOCKET AcceptSocket;
    puts("Waiting for a client to connect...");
    while ((AcceptSocket = accept(srvservice, NULL, NULL)) == INVALID_SOCKET);
    puts("Client Connected.");
    srvservice = AcceptSocket;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)