Playing with winsock2, my first time and having some troubles

Hello everyone!

Here's a 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#define sw_port 27016
#define sw_ip "127.0.0.1"

DWORD WINAPI SocketHandler(void*);
void runserver()
{
	unsigned short wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD( 2, 2 );
 	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )) 
	{
		cout << "Could not find useable sock dll " << WSAGetLastError() << endl;
		return;
	}

	int hsock;
	int *p_int;
	hsock = socket(AF_INET, SOCK_STREAM, 0);
	if(hsock == -1)
	{
		cout << "Error initializing socket " << WSAGetLastError() << endl;
		return;
	}
	
	p_int = new int;
	*p_int = 1;
	if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 ) || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) )
	{
		cout << "Error setting options " << WSAGetLastError() << endl;
		delete p_int;
		return;
	}
	delete p_int;

	struct sockaddr_in my_addr;

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(sw_port);
	
	memset(&(my_addr.sin_zero), 0, 8);
	my_addr.sin_addr.s_addr = INADDR_ANY ;
	
	if( bind( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 )
	{
		cout << "Error binding to socket, make sure nothing else is listening on this port " << WSAGetLastError() << endl;
		return;
	}
	if(listen( hsock, 10) == -1 )
	{
		cout << "Error listening " << WSAGetLastError() << endl;
		return;
	}
	
	int *csock;
	sockaddr_in sadr;
	int	addr_size = sizeof(SOCKADDR);
	
	csock = new int;
	while(true)
	{
		cout << "waiting for a connection..." << endl;
		
		if((*csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size)) != INVALID_SOCKET )
		{
			cout << "Received connection from " << inet_ntoa(sadr.sin_addr) << endl;
			CreateThread(0,0, &SocketHandler, (void*)csock , 0,0);
		}
	}
}

#define format(line, ilen, in,... ) _snprintf( line, ilen, in, __VA_ARGS__ );
int mcounter = 0;
DWORD WINAPI SocketHandler(void* lp)
{
    int *csock = (int*)lp;

	char buffer[1024];
	int buffer_len = 1024;
	int bytecount;

	memset(buffer, 0, buffer_len);
	if( (bytecount = recv(*csock, buffer, buffer_len, 0)) == SOCKET_ERROR)
	{
		cout << "Error receiving data" << WSAGetLastError() << endl;
		return 0;
	}

	cout << "Received bytes:" << bytecount << endl << "Received string:" << buffer << endl;
	format(buffer, 999, "message count(%d)", mcounter);
	mcounter++;
	cout << endl;

	if((bytecount = send(*csock, buffer, strlen(buffer), 0))==SOCKET_ERROR)
	{
		cout << "Error sending data " <<  WSAGetLastError() << endl;
		return 0;
	}
	
	cout << "Sent bytes:" << bytecount << endl;

	free(csock);
    return 0;
}


void runclient()
{
	unsigned short wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested = MAKEWORD( 2, 2 );
 	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )) 
	{
		cout << "Could not find useable sock dll " << WSAGetLastError() << endl;
		return;
	}

	int hsock;
	int * p_int ;
	hsock = socket(AF_INET, SOCK_STREAM, 0);
	if(hsock == -1)
	{
		cout << "Error initializing socket " << WSAGetLastError() << endl;
		return;
	}
	
	p_int = new int;
	*p_int = 1;
	if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 ) || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) )
	{
		cout << "Error setting options " << WSAGetLastError() << endl;
		delete p_int;
		return;
	}
	delete p_int;

	struct sockaddr_in my_addr;

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(sw_port);
	
	memset(&(my_addr.sin_zero), 0, 8);
	my_addr.sin_addr.s_addr = inet_addr(sw_ip);

	if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR )
	{
		cout << "Error connecting socket " << WSAGetLastError() << endl;
		return;
	}

	char buffer[1024] = "";
	int bytecount;
	int re = 0;
	int a = 0;
	while(1)
	{
		a = 0;
		cout << "what do you want to do?" << endl;
		cout << "1- sent string data" << endl;
		cout << "0- disconnect" << endl;
		cin >> re;
		cout << endl << endl;
		cout << "enter a string what you wish to sent" << endl;
		buffer[0] = 0;
		cin >> buffer;
		cout << "sending ..." << endl;
		while(buffer[a]) a++;

		if( (bytecount=send(hsock, buffer, a, 0))==SOCKET_ERROR)
		{
			cout << "Error sending data " << WSAGetLastError() << endl;
			break;
		}

		
		if((bytecount = recv(hsock, buffer, 1024, 0))==SOCKET_ERROR)
		{
			cout << "Error receiving data " << WSAGetLastError() << endl;
			break;
		}
		
		cout << "Recieved bytes:" << bytecount << endl << "Received string:" << buffer << endl;
		cout << endl << endl << endl;
	}
	closesocket(hsock);
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	int mode = 0;
	cout << "choose mode" << endl << endl;
	cout << "1 - server" << endl;
	cout << "2 - client" << endl << endl;
	cin >> mode;
	if( mode == 1 ) runserver();
	else if( mode == 2 ) runclient();
	cout << endl << endl << "press enter to exit..." << endl;
	system("PAUSE");
	return 900; 
}


I'm starting 2 console programs, 1 for server and 1 for client
when im sending a buffer from client first time then everything will be fine
but in second time the app freezes at:
if((bytecount = recv(hsock, buffer, 1024, 0))==SOCKET_ERROR)

I'm assuming that i have to:
 
closesocket(hsock);


and then run the code again.
Pretty much calling runclient() function again.
I think that this is not the way things should be.

I also don't like it that receiving data from server is by calling a function
not like in server code, using a handle.

I am sure that there will be delay receiving data from server
and what does my app do until the data comes?

Does it freezes or just says error receiving data?

Can someone light me on this one?
Thank you!

Also wanted to ask whether it is correct forum for asking this.
I sense that this is about c++ not about c++ related libraries.
I'm not quite sure where should i ask those type of questions.
Without reading the code:
When you try to recv() info the client waits for the server to actually send something, the code will not continue unless it recv()'s something. You need to use threads and make a new thread strictly for recv()ing stuff.

look into CreateThread() or _beginthreadex()
Topic archived. No new replies allowed.