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:

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

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

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

printf(“Invalid input”);

printf(“Invalid input for last argument”);
clientsock = socket(AF_INET, SOCK_STREAM,0); // getting socket
if(clientsock < 0){

desti.sin_family = AF_INET;
desti.sin_port = htons(MYPORT); // htons – short network byte
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 : “);

write (clientsock,ms,200);


return 0;

my client side code:
#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 : “);

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(“″);
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
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
return 0;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: