Need assistance with Recv

Can anyone please explain me how should I write in a command to recive a message from server. I got command Send working. It's on bottom, under 'while'. It's marked with '?????'. Thank you a lot in advance.

Kind regards.

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
#define WIN32_LEAN_AND_MEAN

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#include <string>

// link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_PORT "30003" 
#define DEFAULT_BUFFER_LENGTH	812
using namespace std;

class Client {
public:
	Client(char* servername)
	{
		szServerName = "192.168.0.10";
		ConnectSocket = INVALID_SOCKET;
	}

	bool Start() {
		WSADATA wsaData;

		// Initialize Winsock
		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (iResult != 0)
		{
			printf("WSAStartup failed: %d\n", iResult);
			return false;
		}

		struct addrinfo	*result = NULL,
			*ptr = NULL,
			hints;

		ZeroMemory(&hints, sizeof(hints));
		hints.ai_family = AF_UNSPEC;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;

		// Resolve the server address and port
		iResult = getaddrinfo(szServerName, DEFAULT_PORT, &hints, &result);
		if (iResult != 0)
		{
			printf("getaddrinfo failed: %d\n", iResult);
			WSACleanup();
			return false;
		}

		ptr = result;

		// Create a SOCKET for connecting to server
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Error at socket(): %d\n", WSAGetLastError());
			freeaddrinfo(result);
			WSACleanup();
			return false;
		}

		// Connect to server
		iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);

		if (iResult == SOCKET_ERROR)
		{
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
		}

		freeaddrinfo(result);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Unable to connect to server!\n");
			WSACleanup();
			return false;
		}

		return true;
	};

	// Free the resouces
	void Stop() {
		int iResult = shutdown(ConnectSocket, SD_SEND);

		if (iResult == SOCKET_ERROR)
		{
			printf("shutdown failed: %d\n", WSAGetLastError());
		}

		closesocket(ConnectSocket);
		WSACleanup();
	};

	// Send message to server
	bool Send(char* szMsg)
	{

		int iResult = send(ConnectSocket, szMsg, strlen(szMsg), 0);

		if (iResult == SOCKET_ERROR)
		{
			printf("send failed: %d\n", WSAGetLastError());
			Stop();
			return false;
		}

		return true;
	};

	// Receive message from server
	bool Recv()
	{
		char recvbuf[DEFAULT_BUFFER_LENGTH];
		int iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);

		if (iResult > 0)
		{
			char msg[DEFAULT_BUFFER_LENGTH];
			memset(&msg, 0, sizeof(msg));
			strncpy_s(msg, recvbuf, iResult);

			printf("Received: %s\n", msg);

			cout << recvbuf[0] << "\n";

			return true;
		}


		return false;
	}

private:
	char* szServerName;
	SOCKET ConnectSocket;
};

// KOD
int main(int argc, CHAR* argv[])
{
	string msg, msg1, msg2;
	int i=0;
	char recvbuf;
	Client client("192.168.0.100");

	if (!client.Start())
		return 1;

	msg = "set_digital_out(2, True)\n";
	msg1 = "movel([-0.4235449070518674, ], a=1.2, v=0.25)\n";
	msg2 = "movel([-0.4235449070518674, ], a=1.2, v=0.25)\n";


	

	while (true)
	{
		Sleep(2500);
		client.Send((char*)msg1.c_str());
		Sleep(2500);
		client.Send((char*)msg2.c_str());
		cout << i << "\n";
			i++;
		client.Recv()recvbuf;   ?????? HELP HERE PLS

		cout << recvbuf[0] << "\n";
	}

	client.Stop();

	getchar();
	return 0;
}
I'm sorry.
Instead of having a local varialble msg on line 123 you should provide it as a parameter to Recv().

And instead of returning bool you might return the number of bytes received (i.e. iResult). As soon as you get a value > 0 you can use it.
Thank you so much.
But still, I'm trying with string recived, recived = client.Recv(); but i can't return msg or recvbuf. Why is that? How do i return string into 'main'. Thank you.
Last edited on
What is the problem? Compiler error?

Please show your modified code.
First of all, thank you, I really appreciate your help.
I'm trying to get string, information from port 30003 on UR5 robot (as shown in link below), I'm using following code, but i don't get any back information, why is so?

Kind regards.

http://support.universal-robots.com/Technical/RealTimeClientInterface

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
#define WIN32_LEAN_AND_MEAN

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#include <string>

// link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_PORT "30003" 
#define DEFAULT_BUFFER_LENGTH	812
using namespace std;

class Client {
public:
	Client(char* servername)
	{
		szServerName = "192.168.0.10";
		ConnectSocket = INVALID_SOCKET;
	}

	bool Start() {
		WSADATA wsaData;

		// Initialize Winsock
		int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
		if (iResult != 0)
		{
			printf("WSAStartup failed: %d\n", iResult);
			return false;
		}

		struct addrinfo	*result = NULL,
			*ptr = NULL,
			hints;

		ZeroMemory(&hints, sizeof(hints));
		hints.ai_family = AF_UNSPEC;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;

		// Resolve the server address and port
		iResult = getaddrinfo(szServerName, DEFAULT_PORT, &hints, &result);
		if (iResult != 0)
		{
			printf("getaddrinfo failed: %d\n", iResult);
			WSACleanup();
			return false;
		}

		ptr = result;

		// Create a SOCKET for connecting to server
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Error at socket(): %d\n", WSAGetLastError());
			freeaddrinfo(result);
			WSACleanup();
			return false;
		}

		// Connect to server
		iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);

		if (iResult == SOCKET_ERROR)
		{
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
		}

		freeaddrinfo(result);

		if (ConnectSocket == INVALID_SOCKET)
		{
			printf("Unable to connect to server!\n");
			WSACleanup();
			return false;
		}

		return true;
	};

	// Free the resouces
	void Stop() {
		int iResult = shutdown(ConnectSocket, SD_SEND);

		if (iResult == SOCKET_ERROR)
		{
			printf("shutdown failed: %d\n", WSAGetLastError());
		}

		closesocket(ConnectSocket);
		WSACleanup();
	};

	// Send message to server
	bool Send(char* szMsg)
	{

		int iResult = send(ConnectSocket, szMsg, strlen(szMsg), 0);

		if (iResult == SOCKET_ERROR)
		{
			printf("send failed: %d\n", WSAGetLastError());
			Stop();
			return false;
		}

		return true;
	};

	// Receive message from server
	string Recv()
	{
		char recvbuf[DEFAULT_BUFFER_LENGTH];
		int iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);

		if (iResult > 0)
		{
			char msg[DEFAULT_BUFFER_LENGTH];
			memset(&msg, 0, sizeof(msg));
			strncpy_s(msg, recvbuf, iResult);

			printf("Received: %s\n", msg);


			return msg;
		}


		return 0;
	}

private:
	char* szServerName;
	SOCKET ConnectSocket;
};

// KOD
int main(int argc, CHAR* argv[])
{
	string msg, msg1, msg2;
	int i=0;
	Client client("192.168.0.100");
	string recived;
	


	if (!client.Start())
		return 1;

	//msg = "set_digital_out(2, True)\n";
	msg1 = "movel([-0.4235449070518674], a=1.2, v=0.25)\n";
	msg2 = "movel([-0.4235449070518674], a=1.2, v=0.25)\n";
	

	while (true)
	{
		Sleep(2500);
		client.Send((char*)msg1.c_str());
		Sleep(2500);
		client.Send((char*)msg2.c_str());
		cout << i << "\n";
			i++;

		//msg = this is important part, part where i 
                //recive information from port 30003,
                //but i don't get any

		recived = client.Recv();
		cout << "msg = " << recived << endl;
	}

	client.Stop();

	getchar();
	return 0;
}
Last edited on
but i don't get any back information, why is so?
What do you mean?

As far as I know the default behavior of windows sockets is blocking.
Are you saying the recv(...) doesn't return? Or does it return but it is an empty string?

The link doesn't work since it requires username/password.
Hay again :)

So, this is what the ROBOT port should send me 125 times in a second:

http://postimg.org/image/rf24wo0jd/

I use that port on 30003 and this is what i get:

http://postimg.org/image/hn4iodo8z/

I just need to know robot's position, and I can read it from the given data, thing is, i don't get any i understand. Can you please let me know what I'm doing wrong?

Also, please look at this.

http://postimg.org/image/qvkmybzw5/

On SocketTest, on that port, I don't get any output.
Last edited on
If your first link is the response from the device then it is not a string.

The first four bytes is an int for the length of the message after that an array of double.
Check the length whether the response is what you expected. Note the byte order.

That you get a response is a good sign. Now you need to interpret this response correctly.
Just did it! Just like you said! Thank you a lot once again :)
Topic archived. No new replies allowed.