Hi, I'm trying to learn the in's and out's of using the send and recv functions of a socket in C and I've hit a bit of a road block!

I'm trying to send an integer from one system to another, however one system is big endian (the client) and the other is little endian (the server). My goal for the moment is simple: I want to type an integer in the client and send it to the server and have the server print it out.

Here's the essentials of what I'm trying now:
client.c

struct myPacket {
	int a;
	double b;
	double c;
	int d;
	char e[80];
};
int main(){
	[...]
	struct myPacket msg;
	[...]
	printf("Enter an integer:");
	fflush(stdout);
	scanf("%d",&(msg.a));

	send(sockfd,(void *)msg.a,sizeof(msg.a),0);
	close(sockfd);
	return 0;
}

server.c

[...]
int a;
retval = recv(clientfd, &a, 4, 0);
printf("Message: %d\n", ntohl(a));
[...]

And this gives me garbage. For example, entering "1" on the client returns "415170816" on the server. Also, for the record, I was successfully able to get a character array to pass from the client to the server (that's the char e[80]; in the client code). I was hoping to just have to change the data types but no such luck.

Any help is greatly appreciated!
--Dan

server.c

[...]
int a;
retval = recv(clientfd, &a, 4, 0);
printf("Message: %d\n", ntohl(a));
[...]

Okay, I realized I wasn't initializing a nor was I checking the retval of recv to make sure it was receiving something. retval is receiving 0, which I believe means that no messages are available.

So, this changes my problem slightly...I thought I just wasn't converting the integer correctly, but it seems like I'm not sending it correctly.

Still open for helpful pointers. :-)

I knew I'd figure it out as soon as I posted this!

The problem was in client.c in the send statement. It should read: send(sockfd,&(msg.a),sizeof(msg.a),0);

Edited 6 Years Ago by aiosarem: n/a

This question has already been answered. Start a new discussion instead.