简体   繁体   中英

My python client sends an integer to the c server,but server receives 0

server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <unistd.h>
int main() {
       int s;
       struct sockaddr_in server, client;
       int c, l;
       s = socket(AF_INET, SOCK_STREAM, 0);
       if (s < 0) {
              printf("Eroare la crearea socketului server\n");
              return 1;
       }
       memset(&server, 0, sizeof(server));
       server.sin_port = htons(1234);
       server.sin_family = AF_INET;
       server.sin_addr.s_addr = INADDR_ANY;
       if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
              printf("Eroare la bind\n");
              return 1;
       }
       listen(s, 5);
       l = sizeof(client);
       memset(&client, 0, sizeof(client));
       while (1) {
              int a, b, suma;
              c = accept(s, (struct sockaddr *) &client, &l);
              printf("S-a conectat un client.\n");
              // deservirea clientului
              recv(c, &a, sizeof(a), 0);
              recv(c, &b, sizeof(b), 0);
              a = ntohs(a);
              b = ntohs(b);
              suma = a + b;
              suma = htons(suma);
              send(c, &suma, sizeof(suma), 0);
              close(c);
              // sfarsitul deservirii clientului;
       }
}

Now the idea is simple:the server should receive 2 numbers add them and then send the result back to the client.

client.py

import socket
import struct
import sys

def main():
    client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        client.connect(('localhost',1234))
    except socket.error as error:
        print('Connect error:',error)
        exit(1)
    a=int(input('a='))
    b=int(input('b='))
    try:
        client.sendall(struct.pack('!i',a))
        client.sendall(struct.pack('!i',b))
    except socket.error as error:
        print("Send error:",error)
        exit(1)
    try:
        suma=client.recv(sys.getsizeof(int))
        suma=struct.unpack('!i', suma)[0]
        print("a + b =",suma)
    except socket.error as error:
        print("Receive error:",error)
        exit(1)
if __name__ == '__main__':
    main()

The client sends the numbers correctly,at least i think so,on a python server with the same function it works,and the weird thing is the c client works with the python server,but when i run the c server with the python client,the server receives both a and b with value 0.any ideas?

Tried using a buffer as mentioned below,no result,still receiving 0.

Also,i forgot to mention i checked the value of recv,to be exactly the size of a and b,no recv errors.

Your server invokes ntohs() when it should be using ntohl() . The s (for "short") versions operate on two-byte integers. The l (for "long") versions operate on four-byte integers.

Try this:

          a = ntohl(a);
          b = ntohl(b);

and

          suma = htonl(suma);

Reference: http://linux.die.net/man/3/htons

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM