ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all
This is *not* a serious program, i'm just learning to code. However it does illustrate something that i need to learn how to do.
If i have:
Code:
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
Then how do i send the variables x and y and the character between the two into a buffer for a call to write, in order to write to a socket, so that a host at the other end can just read the buffer at the other end?. The bit that tripped me up was that there's two data types. What is the correct way of doing it as:
void get_player_move(void)
{
int num = 3;
char *buff = malloc(sizeof(int)*3);
int x, y;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x--; y--;
if(matrix[x][y] != ' '){
printf("Invalid move, try again.\n");
get_player_move();
}
/*add code to send move to server*/
else matrix[x][y] = 'X';
buff[0]= (char)x;
buff[1]=' ';
buff[2]= (char)y;
write(sockfd, buff, sizeof(buff));
}
But GCC is still com[plaining about dereferencing pointer to incomplete type! And YES i still get the same error when declaring an int array and casting ' ' to an int like so:
int buff[3];
buff[0]= x;
buff[1]= (int)' ';
buff[2]= y;
I realize that i could just move the problem to the other end(the server) but then i'd still have to do similar on the other end. Is the only solution to just make the program not have a space between the 2 coordinates?
I i do now have some code that solves it this way and uses the non-deprecated socket functions i.e getaddrinfo.
However, just out of interest in C;the way to solve the problem first mentioned is to cast the variable that doesn't fit in the array, is that the only way?
I think it is overcomplicated and unsafe:
scanf (or the program) will die if you enter some "strange" data. You would better check if the input was valid.
You do not need to send the separator, I think the server only need the value of x and y.
Also you have a variable num at the beginning which is not used at all.
this part of the code:
However, just out of interest in C;the way to solve the problem first mentioned is to cast the variable that doesn't fit in the array, is that the only way?
The correct way to mix data types in an object is using a struct rather than an array. You can directly write/read the contents of a struct across the socket as long as the programs on both ends are compiled with compatible compilers/architectures. I expect you are using identical compilers/architectures on the two ends.
I don't understand how the error message you quoted could fit the portion of your code that you asked about. Using (int*) or (char*) when you needed (int) or (char) should give you an error message, but not the one you quoted.
You didn't provide enough code for me to try compiling it myself and see where that error message is coming from.
Thanks for a good reply to an embarrassing trifle matie. Yes it did get cleaned up a bit by not using deprecated syntax. I'll post what i've got so far, if anyone doesn't mind(can be bothered, that is...it might be too much trouble for an app that's purely for my learning purposes...messing around @home etc)
Thankx again
regards
The receiving side can reverse the process and get the correct data.
The alternative can be the use of unions.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
union MyUnion
{
int i;
char c[sizeof(int)];
} x1, y1;
int x, y;
unsigned char buffer[20];
int cnt;
printf("Enter X,Y coordinates for your move: ");
scanf("%d%*c%d", &x, &y);
x1.i=x;
y1.i=y;
memset(buffer, 0x00, 20);
memcpy(&buffer[0],x1.c,sizeof(int));
memcpy(&buffer[0+sizeof(int)],y1.c,sizeof(int));
for(cnt=0;cnt<20;cnt++)
{
printf("%02X ",buffer[cnt]);
if((cnt+1)%10==0)
{
printf("\n");
}
}
return 0;
}
The advantage with this code is that it's independent of the size of the integer. The big disadvantage is that this assumes that both the client and the server run on the same architecture (e.g. Intel X86 family processor versus Motorola 68K family processor); read up on little endian / big endian); not an issue if both server and client run on the same machine. The solution is to convert to network byte order; read 'man htonl'.
Note the difference in the output of the 2 programs. Assuming a 32 bit integer, the bytes will be swapped around when using an Intel/AMD processor.
Last edited by Wim Sturkenboom; 07-22-2012 at 09:37 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.