Winsock2 problem

I noticed in this dll the function gethostbyname() is able to lag the program that calls it on various systems.

I am looking for a solution to this problem.
(Note: I did not write this dll. Just wanting to get it fixed so it no longer lags.)

If you wish to download the full source code for this dll goto:
http://host-a.net/39ster/39dllV25.zip

Here is the source code for that section of 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "socket.h"
int SenderAddrSize = sizeof(SOCKADDR_IN);
SOCKADDR_IN CSocket::SenderAddr;
bool CSocket::tcpconnect(char *address, int port, int mode)
{
	SOCKADDR_IN addr;
	LPHOSTENT  hostEntry;
	if((sockid = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == SOCKET_ERROR)
        return false;
	if((hostEntry = gethostbyname(address))==NULL)
	{
		closesocket(sockid);
		return false;
	}
	addr.sin_family = AF_INET;
	addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
	addr.sin_port = htons((u_short)port);
	if(mode ==2)setsync(1);
	if(connect(sockid, (LPSOCKADDR)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
	{
		if(WSAGetLastError() != WSAEWOULDBLOCK)
		{
			closesocket(sockid);
			return false;
		}
	}
	if(mode ==1)setsync(1);
	return true;
}

bool CSocket::tcplisten(int port, int max, int mode)
{
	if((sockid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) return false;
	SOCKADDR_IN addr;
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons(port);
	if(mode)setsync(1);
	if(bind(sockid, (LPSOCKADDR)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
	{
		closesocket(sockid);
		return false;
	}
	if(listen(sockid, max) == SOCKET_ERROR)
	{
		closesocket(sockid);
		return false;
	}
	return true;
}

CSocket::CSocket(SOCKET sock)
{
	sockid = sock;
	udp = false;
	format = 0;
}
CSocket::CSocket()
{
	udp = false;
	format = 0;
}



CSocket::~CSocket()
{
	if(sockid<0)return;
	shutdown(sockid, 1);
	closesocket(sockid);
}
CSocket* CSocket::tcpaccept(int mode)
{
	if(sockid<0)return NULL;
	SOCKET sock2;
	if((sock2 = accept(sockid, (SOCKADDR *)&SenderAddr, &SenderAddrSize)) != INVALID_SOCKET)
	{
		CSocket*sockit = new CSocket(sock2);
		if(mode >=1)sockit->setsync(1);
		return sockit;
	}
	return NULL;
}

char* CSocket::tcpip()
{
	if(sockid<0)return NULL;
	if(getpeername(sockid, (SOCKADDR *)&SenderAddr, &SenderAddrSize) == SOCKET_ERROR)return NULL;
	return inet_ntoa(SenderAddr.sin_addr);
}

void CSocket::setnagle(bool enabled)
{
	if(sockid<0)return;
	setsockopt(sockid, IPPROTO_TCP, TCP_NODELAY,(char*)&enabled, sizeof(bool));
}

bool CSocket::tcpconnected()
{
	if(sockid<0)return false;
	char b;
	if(recv(sockid, &b, 1, MSG_PEEK) == SOCKET_ERROR)
		if(WSAGetLastError() != WSAEWOULDBLOCK)return false;
	return true;
}

int CSocket::setsync(int mode)
{
	if(sockid < 0)return -1;
	u_long i = mode;
	return ioctlsocket(sockid, FIONBIO, &i);
}

bool CSocket::udpconnect(int port, int mode)
{
	SOCKADDR_IN addr;
	if((sockid = socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
        return false;
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons(port);
	if(mode)setsync(1);
	if(bind(sockid,(SOCKADDR *)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
	{
		closesocket(sockid);
		return false;
	}
	udp = true;
	return true;
}

int CSocket::sendmessage(char *ip, int port, CBuffer *source)
{

	if(sockid<0)return -1;
	int size = 0;
	SOCKADDR_IN addr;
	if(udp)
	{
		size = min(source->count, 8195);
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
		addr.sin_addr.s_addr = inet_addr(ip);
		size = sendto(sockid, source->data, size, 0, (SOCKADDR *)&addr, sizeof(SOCKADDR_IN));
	}
	else
	{
		CBuffer sendbuff;
		sendbuff.clear();
		if(format == 0)
		{
			sendbuff.writeushort(source->count);
			sendbuff.addBuffer(source);
			size = send(sockid, sendbuff.data, sendbuff.count, 0);
		}else if(format == 1)
		{
			sendbuff.addBuffer(source);
			sendbuff.writechars(formatstr);
			size = send(sockid, sendbuff.data, sendbuff.count, 0);
		}else if(format == 2)
			size = send(sockid, source->data, source->count, 0);
	}
	if(size == SOCKET_ERROR)return -WSAGetLastError();
	return size;
}

int CSocket::receivetext(char*buf, int max)
{
	int len = (int)strlen(formatstr);
	if((max = recv(sockid, buf, max, MSG_PEEK)) != SOCKET_ERROR)
	{
		int i, ii;
		for(i = 0; i < max; i ++)
		{
			for(ii = 0; ii < len; ii++)
				if(buf[i+ii] != formatstr[ii])
					break;
			if(ii == len)
				return recv(sockid, buf, i + len, 0);
		}
	}
	return -1;
}
int CSocket::receivemessage(int len, CBuffer*destination)
{
	if(sockid<0)return -1;
	int size = -1;
	char* buff = NULL;
	if(udp)
	{
		size = 8195;
		buff = new char[size];
		size = recvfrom(sockid, buff, size, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
	} else
	{
		if(format == 0 && !len)
		{
			unsigned short length;
			if(recv(sockid, (char*)&length, 2, 0) == SOCKET_ERROR)return -1;
			buff = new char[length];
			size = recv(sockid, buff, length, 0);
		} else if(format == 1 && !len)
		{
			size = 65536;
			buff = new char[size];
			size = receivetext(buff, size);
		} else if(format == 2 || len > 0)
		{
			buff = new char[len];
			size = recv(sockid, buff, len, 0);
		}
	}
	if(size > 0)
	{
		destination->clear();
		destination->addBuffer(buff, size);
	}
	if(buff != NULL)delete buff;
	return size;
}

int CSocket::peekmessage(int size, CBuffer*destination)
{
	if(sockid<0)return -1;
	if(size == 0)size = 65536;
	char *buff = new char[size];
	size = recvfrom(sockid, buff, size, MSG_PEEK, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
	if(size < 0)
	{
		delete buff;
		return -1;
	}
	destination->clear();
	destination->addBuffer(buff, size);
	delete buff;
	return size;
}

int CSocket::lasterror()
{
	return WSAGetLastError();
}

char* CSocket::GetIp(char*address)
{
	SOCKADDR_IN addr;
	LPHOSTENT hostEntry;
	if((hostEntry = gethostbyname(address)) == NULL) return NULL;
	addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
	return inet_ntoa(addr.sin_addr);
}

char* CSocket::lastinIP(void)
{
	return inet_ntoa(SenderAddr.sin_addr);
}

unsigned short CSocket::lastinPort(void)
{
	return ntohs(SenderAddr.sin_port);
}

int CSocket::SetFormat(int mode, char* sep)
{
	int previous = format;
	format = mode;
	if(mode == 1 && strlen(sep)>0)
		strcpy(formatstr, sep);
	return previous;
}

int CSocket::SockExit(void)
{
	WSACleanup();
	return 1;
}
int CSocket::SockStart(void)
{
	WSADATA wsaData;
	WSAStartup(MAKEWORD(1,1),&wsaData);
	return 1;
}

char* CSocket::myhost()
{
	static char buf[16];
	gethostname(buf, 16);
	return buf;
}
What do you mean by lag?
While the program is connecting to a server it will freeze the entire program.
Yes, but for how long? Have you thought about what gethostbyname() has to do? It usually involves a DNS/WINS lookup, and so network configuration will affect your response time.
It lasts for 5 to 10 secs normally. On my other programs like World of Warcraft, Warcraft 3, Diablo 2, Starcraft, etc. I will connect with no lag.

I tested the dll trying to connect to just google.com and it froze.

I did many pings to things like google.com, danopia.no-ip.org
and many others

Google.com returned with pings around 40ms
While danopia.no-ip.org returned with pings around 100ms
I pinged danopia.no-ip.org on my friend's pc which isn't even in my network, house, or even in the same state. I got pings around 100ms for danopia.no-ip.org on my friend's pc. I also got pings for wowcrack.servegame.com on my friend's pc around 100ms.

Both danopia.no-ip.org and wowcrack.servegame.com are hosted by a site called no-ip.com
Topic archived. No new replies allowed.