python 2.7 - Receiving data from Network C++ -
i have python function send data on network data gram socket.in receiver end have use c++ receive data receiver can't receive meaningful data.what should used string in exact format sent?
python code
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 ip address h2 = map(int, segments[2].split('.'))# segment 2 contains ip address fmt = '4s4b4bh' + segments[3] + 's'# segemt 3 string length segment 4 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 {0}'.format(segments[1])) # print message before sending source self.s.sendto(buf, (segments[1], self.port)) # send source #self.s.sendto(command, (segments[1], self.port)) # send source c++ code
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); all sockets created properly
if receive "send ???????" i'd expect encoding problems during sending. '?' 0x3f (dec 63) comes characters out of basic ascii (less 127) table. i'm not familiar python, can ensure use proper (e.g. utf-8, sure not utf-16/32 , perhaps not ascii) encoding while transforming python string byte array?
Comments
Post a Comment