Hi, me AGAIN!
Having trouble getting an integer sent from my client to my server...everything compiles nicely, but nothing seems to be getting recieved....
As soon as I can get some data being sent back and forth, I am well way...
Client Code
Code:
/*Socket*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
int sd;
int main(int argc,char** argv)
{
int a = 0;
sd = socket(AF_INET, SOCK_STREAM,0);
/*End of Socket*/
/*Client*/
struct sockaddr_in servaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET; //Set the domain
servaddr.sin_port = htons(atoi(argv[2])); //The port to connect to
inet_aton(argv[1],&servaddr.sin_addr); //THe IP address of the server machine.
//e.g Client 127.0.0.1 5000
connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("Please enter the integer you wish to send ");
scanf("%d", &a);
send(sd, &a, sizeof(a), 0);
/*End of Client*/
}
//SOCK_STREAM creates a realiable two-way connection
//AF_INET specifies IPv4 Internet protocols
//s is the descriptor (sd)
//buf is the data to send (e.g a string)
//len is the length of data sent
//flags are the options(in this case can be 0).
This is my server
Code:
/*Socket*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
int sd = 0;
int main(int argc,char** argv)
{
int connfd = 0;
int listenfd = 0;
size_t clilen;
int recv_data = 0; //recieving integer data
sd = socket(AF_INET, SOCK_STREAM,0); //Creating New Socket
listenfd = sd;//Did this as listenfd has not been defined yet
/*End of Socket*/
/*Server*/
struct sockaddr_in servaddr; //Creating buffer
bzero(&servaddr,sizeof(servaddr)); //Sets all values in buffer to zero
servaddr.sin_family = AF_INET; //Sets the domain
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //Accepting connections from any machine
servaddr.sin_port = htons(atoi(argv[1])); //First argument, e.g Server 50,000 uses port 50,000
bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); //Binds a socket to an address
listen(listenfd,10); //Accepts 10 clients to connect
int store;
//infinite for loop HERE
//used to accept connections from client
for(;;) {
struct sockaddr_in cliaddr;
connfd = accept(listenfd,(struct sockaddr*)&cliaddr,&clilen); //-1 if nothing is being received
store = recv(sd, &recv_data, sizeof(recv_data), 0);
if (connfd != -1)
{
printf("%d", store);
}
/*End of Client*/
}
}
//SOCK_STREAM creates a realiable two-way connection
//AF_INET specifies IPv4 Internet protocols
//listenfd is a descriptor for the servers socket(intiated when creating the socket)
I assume that when I return information back from the server, I 'send' from the server, and 'recv' at the client...
Does this mean I'd need another infinite for loop at the client, always checking wther information is being sent back or not?
Thanks