Tuesday, December 18, 2007

C Socket Programming - Client-Server

i have been a blogger for posting a lot of things almost 3 years now and i havent posted anything that is related to what my profession is right now (huwaw! so i just considered myself a professional eh!). i mean, i haven't posted a lot of techie stuffs here yet. that being said, im going to start making it come to life now (i mean, posting some technical stuffs). who knows? i might be able to help other people by posting things? goddamn! i talk too much! eheheh...

well, i have been into a lot of networking and programming in C and C++ for almost 8 months now. hmmm, sorry, make that 7 months. and i can say that i am very much enjoying it. last week, i have studied a little about socket programming (eehheh, i take pride, coz before, i didnt even know what socket programming was! thank god for the opportunity!) i, with some of my friends-slash-housemates-slash-c0-workers, did a simple client-server communication. the main aim was that the client will be sending a message to the server. geez! it took a lot of mind boggling for me. but nevertheless, I was able to finish it (well, hard core programmers may perceive this code really messy. some would even say, it sucks! eheheh. but i am still proud to what I have came up with. this drives me more to aim and do more...), of course with the help of my friends-slash-slash-slash.

and without further ado (hmm, i would've said, with a lot of things said! dang!ehhehe), this was what i have came up with:


------------------------------------------------------------------------------------

//THIS PROGRAM IS FOR THE SERVER

//server.c sample server

#include < stdio.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < sys/types.h >
#define MYPORT 9993
#define BACKLOG 5
#define MAXDSIZE 50

int main(void)
{
int sockfd, newsockfd; //listens of sock_fd, new connection on new_fd
struct sockaddr_in my_addr; //server's address info
struct sockaddr_in client_addr; // connector's address info
char * ip;
char buf[MAXDSIZE];
int * memsock;

int n;

if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error");
exit(1);
}

//initialization of values
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); //short network byte order
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //my own IP address
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

//bind error checking

if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof (my_addr))) == -1)
{
perror("bind error");
exit(1);
}


/*this is the part where i was not able to focus on. this should reuse the port address the server used to listen to. therefore, i was not able to utilize this code

if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
} */

//listen error checking
if (listen(sockfd, BACKLOG) == -1)
{
perror("listen error");
exit(1);
}

//accept

n = sizeof(client_addr);

newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &n);


memset(buf, '\0', MAXDSIZE);

//receive
if(recv(newsockfd, buf, MAXDSIZE, 0) == -1)
{
perror("receive error");
exit(1);
}


printf("%s", buf);
close (sockfd);

return 0;
}







------------------------------------------------------------------------------

//THIS IS FOR THE CLIENT

//sclient.c sample server

#include < stdio.h >
#include < stdlib.h >
#include < sys/socket.h >
#include < netinet/in.h >
#include < sys/types.h >

#define MAXDSIZE 50

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

int sockfd;
int myport;
struct sockaddr_in server_addr;
char *msg;



if(argc != 3)
printf("Input is incomplete");



if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket error");
exit(1);
}

//initialization of values
myport = atoi(argv[2]);
server_addr.sin_family = AF_INET; // host byte order
server_addr.sin_port = htons(myport); //short network byte order

inet_aton(argv[1], &server_addr.sin_addr);


memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero);



//connect
if((connect(sockfd, (struct sockaddr_in *)&server_addr, sizeof(server_addr))) == -1)
{
perror("connect error");
exit(1);
}

//message, send message
printf("Enter your message: ");

memset(msg, '\0', MAXDSIZE-1);
fgets(msg, MAXDSIZE, stdin);
write(sockfd, msg, MAXDSIZE);


//close
close (sockfd);


return 0;
}




well, i know what i did was way too basic. and i am a beginner. eheheh... : )

more to come....

2 Comments:

Anonymous Anonymous said...

that's a good start dude. goodluck

8:07 PM  
Blogger Unknown said...

thanks finch! : )

7:23 PM  

Post a Comment

<< Home