My first network program.wweeeeee…

Posted in PROGRAMMING by edeguzman on December 16, 2007

This is my first time to create a client/server program.  But this is only the basic, sending a message to the server.  My code below shows that the server and client is on my own workstation. It uses a localhost address, i use the localhost address b’c0z i d0nt want to apply it in a remote host without having it done perfectly fine in my own machine, but the concept is still the same, you only need to change the address.

Pls do consider my code even its quite messy. I’m still a newbie still learning the basics of socket programming.

my Server side code:
#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[]){

struct sockaddr_in desti; // my address
int clientsock; //my socket to listen
int MYPORT;

if(argc != 3){
printf(“usage should be a.out filename”);
}

if(argv[1]==NULL){
printf(“Invalid input”);
exit(1);
}

if(argv[2]==NULL){
printf(“Invalid input for last argument”);
}
else{
MYPORT=atoi(argv[2]);
}
clientsock = socket(AF_INET, SOCK_STREAM,0); // getting socket
if(clientsock < 0){
printf(“error”);
}

desti.sin_family = AF_INET;
desti.sin_port = htons(MYPORT); // htons – short network byte
inet_aton(argv[1],&(desti.sin_addr));
memset(desti.sin_zero,”,sizeof desti.sin_zero); // setting sin_zero
to 0

connect(clientsock, (struct sockaddr *)&desti, sizeof(desti));

char ms[200],ch;
int i=0;
printf(“Enter your message : “);
ch=getchar();
while(ch!=’n’){
ms[i]=ch;
ch=getchar();
i++;
}

write (clientsock,ms,200);

shutdown(clientsock,2);
close(clientsock);

return 0;
}

my client side code:
#include
#include
#include
#include
#include
#include
#define log 5

int main(){

struct sockaddr_in my_addr; // my address
struct sockaddr_in sender_addr; // sender’s address
int mysock; // my socket to listen
int callerfd; // sender accepting socket
int sender; // size of sender’s address
int LEN = 255;
char rec_msg[LEN];

int myport;
printf(“Enter the port connection : “);
scanf(“%d”,&myport);

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(myport); // htons – host to short
network byte
my_addr.sin_addr.s_addr = inet_addr(“127.0.0.1″);
memset(my_addr.sin_zero,”,sizeof my_addr.sin_zero); // setting
sin_zero to 0

if ((mysock = socket(PF_INET, SOCK_STREAM,0)) < 0) // getting
socket state 1
{
printf(“Error in state 1”);
}
int testing;
testing = bind(mysock, (struct sockaddr *)&my_addr, sizeof my_addr);
// binding function state 2
if(testing <0)
{
printf(“Error in state 2”);
}
if (listen(mysock,log) < 0) // listens at port state 3
{
printf(“Error in state 2”);
}
sender = sizeof sender_addr;
callerfd = accept(mysock,(struct sockaddr *)&sender_addr, &sender);
// making connection state 4
if(callerfd <0){
printf(“Error in state 4”);
}

if (recv(callerfd,rec_msg,LEN,0) < 0) // receiving msg state
5
{
printf(“Error in state 5”);
}
printf(“nn %s nn”,rec_msg);
}

shutdown(mysock,2); // shutting down socket state 6
close(mysock); // closing socket connection state 7
close(callerfd);
return 0;
}