winsocket problem, 2 accept() for 1 client?

Hello everyone!

Here's my socket class:
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
class spSocket
{
public:
	SOCKET psocket;
	spchar *ip;
	spchar *port;
	spchar *error;

	spSocket()
	{
		port = 0;
		ip = new spchar(20);
		port = new spchar(20);
		error = new spchar(256);
		psocket = INVALID_SOCKET;
	}

	unsigned int invalid()
	{
		return INVALID_SOCKET;
	}

	int sendmessage(SOCKET sin, void *in, int size)
	{
		int r = send(sin, (char *)in, size, 0);
		return r;
	}

	int receivemessage(SOCKET sin, void *out, int size)
	{
		int r = recv(sin, (char *)out, size, 0);
		return r;
	}

	SOCKET WaitForClient()
	{
		return accept(psocket, NULL, NULL);
	}

	void CloseConnection()
	{
		if (psocket == INVALID_SOCKET) return;
		closesocket(psocket);
		psocket = INVALID_SOCKET;
	}

	void CloseConnection(SOCKET in)
	{
		if (in != INVALID_SOCKET) closesocket(in);
		if (psocket == INVALID_SOCKET) return;
		closesocket(psocket);
		psocket = INVALID_SOCKET;
	}

	bool CreateServer(spchar IP, spchar PORT)
	{
		*ip = IP;
		*port = PORT;
		WSADATA wsaData;
		psocket = INVALID_SOCKET;

		struct addrinfo *result = NULL;
		struct addrinfo hints;

		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (iResult != 0) {
			error->format("WSAStartup failed with error: %d\n", iResult);
			return false;
		}

		ZeroMemory(&hints, sizeof(hints));
		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;
		hints.ai_flags = AI_PASSIVE;

		if (ip->size) iResult = getaddrinfo(ip->getchar(), port->getchar(), &hints, &result);
		else iResult = getaddrinfo(NULL, port->getchar(), &hints, &result);

		if (iResult != 0) {
			error->format("getaddrinfo failed with error: %d\n", iResult);
			WSACleanup();
			return false;
		}

		psocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

		if (psocket == INVALID_SOCKET) {
			error->format("socket failed with error: %ld\n", WSAGetLastError());
			freeaddrinfo(result);
			WSACleanup();
			return false;
		}

		if (iResult == SOCKET_ERROR) {
			error->format("ioctlsocket failed with error: %d\n", WSAGetLastError());
			closesocket(psocket);
			WSACleanup();
			return false;
		}

		iResult = bind(psocket, result->ai_addr, (int)result->ai_addrlen);
		if (iResult == SOCKET_ERROR) {
			error->format("bind failed with error: %d\n", WSAGetLastError());
			freeaddrinfo(result);
			closesocket(psocket);
			WSACleanup();
			return false;
		}

		freeaddrinfo(result);
		iResult = listen(psocket, SOMAXCONN);

		if (iResult == SOCKET_ERROR) {
			error->format("listen failed with error: %d\n", WSAGetLastError());
			closesocket(psocket);
			WSACleanup();
			return false;
		}
		return true;
	}

	bool Connect(spchar IP, spchar PORT)
	{
		*ip = IP;
		*port = PORT;
		WSADATA wsaData;
		struct addrinfo *result = NULL, *ptr = NULL, hints;
		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

		if (iResult != 0) {
			error->format("WSAStartup failed with error: %d\n", iResult);
			return false;
		}

		ZeroMemory(&hints, sizeof(hints));
		hints.ai_family = AF_UNSPEC;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;
		iResult = getaddrinfo(ip->getchar(), port->getchar(), &hints, &result);

		if (iResult != 0)
		{
			error->format("getaddrinfo failed with error: %d\n", iResult);
			WSACleanup();
			return false;
		}

		for (ptr = result; ptr != NULL; ptr = ptr->ai_next) 
		{
			psocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

			if (psocket == INVALID_SOCKET) {
				error->format("socket failed with error: %ld\n", WSAGetLastError());
				WSACleanup();
				return false;
			}

			iResult = connect(psocket, ptr->ai_addr, (int)ptr->ai_addrlen);

			if (iResult == SOCKET_ERROR)
			{
				closesocket(psocket);
				psocket = INVALID_SOCKET;
				error->format("The server is down... did not connect");
			}
		}

		freeaddrinfo(result);

		if (psocket == INVALID_SOCKET)
		{
			error->format("Unable to connect to server!\n");
			WSACleanup();
			return false;
		}

		if (iResult == SOCKET_ERROR)
		{
			error->format("ioctlsocket failed with error: %d\n", WSAGetLastError());
			closesocket(psocket);
			WSACleanup();
			return false;
		}
		return true;
	}

	~spSocket()
	{


	}
};


This is how i'm using it:
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
int _tmain(int argc, _TCHAR* argv[])
{
	/*cout << "1- create server" << endl;
	cout << "2- create client" << endl;*/

	cout << "3- create simple server" << endl;
	cout << "4- create simple client" << endl;
	cout << endl << endl;
	int o = 1;
	running = 0;
	cin >> o;

	if (o == 3)
	{
		char buffer[128];
		spSocket a;
		a.CreateServer("", "27000");
		SOCKET t = a.WaitForClient();
		/*
		This last WaitForClient should block my program and wait for another connection
		but it doesn't do that.
		The last one returns valid client SOCKET but the first one is unknown for me.
		*/
		t = a.WaitForClient(); 

		a.receivemessage(t, buffer, 128);
		cout << "got message:" << buffer << endl;

		cin >> buffer;
		cout << "sending:" << buffer << endl;
		a.sendmessage(t, buffer, 128);
		system("pause");
		return 0;
	}

	if (o == 4)
	{
		char buffer[128];
		spSocket a;
		a.Connect("", "27000");
		cin >> buffer;
		cout << "sending:" << buffer << endl;
		a.sendmessage(a.psocket, buffer, 128);

		a.receivemessage(a.psocket, buffer, 128);
		cout << "got message:" << buffer << endl;
		system("pause");
		return 0;
	}
	system("pause");
	return 0;
}


Thanks!
Last edited on
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
		/* i
		nside:
		bool Connect(spchar IP, spchar PORT) 
		*/

		for (ptr = result; ptr != NULL; ptr = ptr->ai_next) 
		{
			psocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

			if (psocket == INVALID_SOCKET) {
				error->format("socket failed with error: %ld\n", WSAGetLastError());
				WSACleanup();
				return false;
			}

			iResult = connect(psocket, ptr->ai_addr, (int)ptr->ai_addrlen);

			if (iResult == SOCKET_ERROR)
			{
				closesocket(psocket);
				psocket = INVALID_SOCKET;
				error->format("The server is down... did not connect");
				continue;
			}
			break // <-- forgot to break.
		}
Last edited on
Shouldn't you also add continue; after error->format("The server is down... did not connect"); ?
Last edited on
oh yeah thanks.
I already did it actually in my code back in project yet i forgot to bring that update here.
Topic archived. No new replies allowed.