Problem with socket

Hi men!
I have a problem with socket in visual studio
When i run this code and sent with a client (the client isn't the problem) some byte, it start to write on screen "byte recv -1" endlessy...here the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
void main()
{
	printf("############################## SERVER #####################################\n\n");
	temp = WSAStartup(MAKEWORD(2,2),&data);
	if(temp != NO_ERROR){
      printf("Error in WSAStartup");
	  system("PAUSE");
	}
	
	ZeroMemory(&hints,sizeof(hints));
	hints.ai_family= AF_INET;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_socktype = SOCK_STREAM;
	

	sockAdd = &hints;

	temp = getaddrinfo(NULL,DEFAULT_PORT,&hints,&sockAdd);
	if (temp  < 0 ){
	printf("Getaddrinfo error");
	system("PAUSE");
	}
    
    
		master_sock = socket(sockAdd->ai_family,sockAdd->ai_socktype,sockAdd->ai_protocol);
	if (master_sock == INVALID_SOCKET){
	printf("Error in creating socket");
	system("PAUSE");
	}
	  
	
		 if(setsockopt (master_sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val)) == SOCKET_ERROR){
	      printf("error with setsockopt func");
		 system("PAUSE");
		 }


	
		 temp = bind(master_sock,sockAdd->ai_addr,sockAdd->ai_addrlen);
		if(temp == SOCKET_ERROR){
	     printf("Error in bind %d\n",WSAGetLastError());
	      system("PAUSE");
		}
	
		temp = listen(master_sock,3);
		if (temp < 0)
		{
			printf("Error with listen func");
			system("PAUSE");
		}
		

		sockaddr from;
		SOCKET client;
		

		while(true){
		FD_ZERO(&fds);
		FD_SET(master_sock,&fds);

		if(select(4,&fds,NULL,NULL,NULL) > 0){
		
		if(FD_ISSET(master_sock,&fds)){
			client = accept(master_sock,(sockaddr *)&from,(int *)sizeof(from));
		do{
		sendResult = recv(client,recvBuffer,sizeof(recvBuffer),NULL);
			printf("Byte recv %d",sendResult);
		}while (sendResult > 0);
		
		}

		}



		}

		}



OK,OK...probably i did a mess with fd_set and select function but actually i'm not able to solve this on my own.
Thank to anyone able to solve this! : )
Last edited on
closed account (jwkNwA7f)
Can I see the rest of your code?
closed account (Dy7SLyTq)
what library are you using? it doesnt like the standard c library. are you sure that it is actually communicating with the client? are both running? how are calling each program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifdef UNICODE
#undef UNICODE
#endif
#ifdef _UNICODE
#undef _UNICODE
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#define MAX_SOCK 3

#include "stdafx.h"
#include <Windows.h>
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment (lib,"Ws2_32.lib")

sockaddr addInfo;
WSAData data;
addrinfo hints,
	*sockAdd;
int temp,count,sendResult;
char recvBuffer[DEFAULT_BUFLEN];
fd_set fds;
SOCKET master_sock;
void HandleRequest(int time);
char val = TRUE;


These are the lib declaration, i use visual studio (C++\CLI), and the code is for a multiconnection server... if you need i can post also the client code but it actually work..so...tell me if you need it.
Last edited on
I've fixed the indentation on your code so I can see what you've done. Hopefully the problems will be apparent to you too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
void main()
{
	printf("############################## SERVER #####################################\n\n");
	temp = WSAStartup(MAKEWORD(2,2),&data);
	if(temp != NO_ERROR)
	{
		printf("Error in WSAStartup");
		system("PAUSE");
	}
	
	ZeroMemory(&hints,sizeof(hints));
	hints.ai_family= AF_INET;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_socktype = SOCK_STREAM;
	sockAdd = &hints;

	temp = getaddrinfo(NULL,DEFAULT_PORT,&hints,&sockAdd);
	if (temp < 0 )
	{
		printf("Getaddrinfo error");
		system("PAUSE");
	}

	master_sock = socket(sockAdd->ai_family,sockAdd->ai_socktype,sockAdd->ai_protocol);
	if (master_sock == INVALID_SOCKET)
	{
		printf("Error in creating socket");
		system("PAUSE");
	}

	if (setsockopt (master_sock,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val)) == SOCKET_ERROR)
	{
		printf("error with setsockopt func");
		system("PAUSE");
	 }

	temp = bind(master_sock,sockAdd->ai_addr,sockAdd->ai_addrlen);
	if (temp == SOCKET_ERROR)
	{
		printf("Error in bind %d\n",WSAGetLastError());
		system("PAUSE");
	}
	
	temp = listen(master_sock,3);
	if (temp < 0)
	{
		printf("Error with listen func");
		system("PAUSE");
	}

	sockaddr from;
	SOCKET client;

	while (true)
	{
		FD_ZERO(&fds);
		FD_SET(master_sock,&fds);

		if (select(4, &fds, NULL, NULL, NULL) > 0)
		{
			if (FD_ISSET(master_sock,&fds))
			{
				client = accept(master_sock,(sockaddr *)&from,(int *)sizeof(from));

				do
				{
					sendResult = recv(client,recvBuffer,sizeof(recvBuffer),NULL);
					printf("Byte recv %d",sendResult);
				}
				while (sendResult > 0);
			}
		}
	}
}


You are attempting to multiplex the handling of the client(s) and server connections. But you're not doing it correctly.

select() tells you if a socket is available for I/O or Error. For it to do so, you must pass it socket descriptors and the value of the maximum socket value + 1. It uses this as the set of sockets to process.

When select returns, you need to check the socket handles to see if they're ready to read, then do the read.

This implies that the set of sockets changes as clients connect and disconnect.

When a client connects, the server socket will be available to read. That's where you call accept(), add the client to the array and continue around the loop.

When a client disconnects, the server should remove the client from the list and continue around the loop.

As you can see, you're not really doing that.
I fix the "while" part of the code but it still doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
while (true)
	{
		FD_ZERO(&fds);
		FD_SET(master_sock,&fds);
        activity = select(master_sock + 1,&fds,NULL,NULL,NULL);
		if(FD_ISSET(master_sock,&fds)){
			client = accept(master_sock,(sockaddr *)&addInfo,(int *) sizeof(addInfo.sin_addr));
			do {
				sendResult = recv(client,recvBuffer,sizeof(recvBuffer),NULL);
				printf("byte recv %d",sendResult);
			}while(sendResult > 0);
		break;
		}


- I set master_sock in the fds set
- Then i call select function
- After that i check if master_sock it's still inside the fds (so i know that it's readable)
-I do recv stuff.
But it still show "Byte recv -1 " endlessy.
.... it's like if i don't understend something.
Thanks for help
You need to fix that indentation or you'll never be quite sure what you've written.

The workflow is wrong. The psuedo code looks like this.
1
2
3
4
5
6
7
8
9
10
11
while stop flag not set
    add server socket to collection
    build an fds set from the collection
    call select
    for each socket in collection
        if can read
            if server socket
                accept new connection
                add it to collection
            else
                handle client request

Last edited on
Topic archived. No new replies allowed.