0
我有一個Python功能在網絡上通過數據克socket.In接收機端我必須使用C++來接收數據但是接收機不能接收任何有意義的data.What應該發送數據接收的數據用於獲取發送的確切格式的字符串?從網絡C++
Python代碼
def send(self, command):
segments = command.split() # split command on spaces
if len(segments) < 5 or segments[1] not in self.hosts or segments[2] not in self.hosts: # sanity check
print('invalid arguments')
return
else:
h1 = map(int, segments[1].split('.'))# segment 1 contains a IP address
h2 = map(int, segments[2].split('.'))# segment 2 contains an IP address
fmt = '4s4B4Bh' + segments[3] + 's'# segemt 3 is string length segment 4 is a string
buf = struct.pack(fmt, segments[0], h1[0], h1[1], h1[2], h1[3], h2[0], h2[1], h2[2], h2[3], int(segments[3]), ' '.join(segments[4:]))
print('sending to {0}'.format(segments[1])) # print a message before sending to source
self.s.sendto(buf, (segments[1], self.port)) # send to source
#self.s.sendto(command, (segments[1], self.port)) # send to source
C++代碼
bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*) &client_address, &addrlen);
//print(buffer);
if(bytes_received==-1)
{
perror("recvfrom");
printf("error %s\n",strerror(errno));
}
printf("[%s:%hu]: %s \n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port), buffer);
所有插座正確創建
顯示buffer'的'的聲明。我懷疑'sizeof(buffer)'是不正確的。 –
你有一個關於Python代碼使用的數據格式的規範嗎?如果沒有,你應該開始做一個。 –
緩衝區是一個字符數組,其大小1024是不是足夠收到這樣的消息「送192 168 10 1 11參考hello world」 – shuvo