Sign in to follow this  
Geel_jire

socket programming

Recommended Posts

Salaam all.

 

I know there are alot of programmers on the site.

so brothers and sisters i just need some advice.

 

if you have experience with socket programming(strictly C ) in a UNIX environment or any other POSIX compliant operating system.

 

what books/site/tools would you recommend ?

 

havent done any programming for a while I'm more of a network guy, but i do have the basic of C down just need to brush up on pointers and struct's.

 

any input would be appreciated !

Share this post


Link to post
Share on other sites

Hey G, what do want to know exactly. You dont really need to do any raw socket processing these days unless your after some measurement / performance and etc. there are plenty of libraries that provide you with wrappers to abstract away the lower-level stuff.

 

Again, it is the same with C, unless you *must* use C, don't. Higher-level languages like python, and ruby are posix complaint and make it really easy to write even low-level socket processing.

 

For example here, is a complete low-level socket transceiver (server and client) in python (tutorial i got this from http://www.amk.ca/python/howto/sockets/)

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

code:

import os, socket                                                               

class mysocket:

'''demonstration class only

- coded for clarity, not efficiency'''

def __init__(self, sock=None):

if sock is None:

self.sock = socket.socket(

socket.AF_INET, socket.SOCK_STREAM)

else:

self.sock = sock

def connect(host, port):

self.sock.connect((host, port))

def mysend(msg):

totalsent = 0

while totalsent < MSGLEN:

sent = self.sock.send(msg[totalsent:])

if sent == 0:

raise RuntimeError,

"socket connection broken"

totalsent = totalsent + sent

 

def myreceive():

msg = ''

while len(msg) < MSGLEN:

chunk = self.sock.recv(MSGLEN-len(msg))

if chunk == '':

raise RuntimeError, "socket connection broken"

msg = msg + chunk

return msg

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

 

The same code in C (tutorial http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html)

 

The server:

----------

code:

/* a server in the unix domain.  The pathname of 

the socket address is passed as an argument */

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <stdio.h>

void error(char *);

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

{

int sockfd, newsockfd, servlen, clilen, n;

struct sockaddr_un cli_addr, serv_addr;

char buf[80];

 

if ((sockfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)

error("creating socket");

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sun_family = AF_UNIX;

strcpy(serv_addr.sun_path, argv[1]);

servlen=strlen(serv_addr.sun_path) +

sizeof(serv_addr.sun_family);

if(bind(sockfd,(struct sockaddr *)&serv_addr,servlen)<0)

error("binding socket");

 

listen(sockfd,5);

clilen = sizeof(cli_addr);

newsockfd = accept(

sockfd,(struct sockaddr *)&cli_addr,&clilen);

if (newsockfd < 0)

error("accepting");

n=read(newsockfd,buf,80);

printf("A connection has been establishedn");

write(1,buf,n);

write(newsockfd,"I got your messagen",19);

}

 

void error(char *msg)

{

perror(msg);

exit(0);

}

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

 

The client

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

code:

/* a client in the unix domain */

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <stdio.h>

void error(char *);

 

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

{

int sockfd, servlen,n;

struct sockaddr_un serv_addr;

char buffer[82];

 

bzero((char *)&serv_addr,sizeof(serv_addr));

serv_addr.sun_family = AF_UNIX;

strcpy(serv_addr.sun_path, argv[1]);

servlen = strlen(serv_addr.sun_path) +

sizeof(serv_addr.sun_family);

if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)

error("Creating socket");

if (connect(sockfd, (struct sockaddr *)

&serv_addr, servlen) < 0)

error("Connecting");

printf("Please enter your message: ");

bzero(buffer,82);

fgets(buffer,80,stdin);

write(sockfd,buffer,strlen(buffer));

n=read(sockfd,buffer,80);

printf("The return message wasn");

write(1,buffer,n);

}

 

void error(char *msg)

{

perror(msg);

exit(0);

}

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

 

As you can see the python version is much smaller, and easier to understand. Anyhow, if you must, there are plenty of C tutorials online and for an in depth understanding, the daddy of all such books are:

W. Richard Stevens, Unix Network Programming series.

The have in depth discussions of protocols and performance, i/o and all the weird little variable that make you scream whhaattttt!

Share this post


Link to post
Share on other sites

Thank you for the reply bro.

 

it is for a course i'm taking.

with a very finicky prof.

 

he insists "you must use strictly ANSI C , on any POSIX compliant OS"

 

used to love programming ,but was too much work smile.gif

have to go back and brush up on a few things

 

I will check out the tutorial and book. thnx

Share this post


Link to post
Share on other sites

cool, well at the very least, c socket programming is the best way to learn the stuff, -- given that most libraries/language implementations and real production code is based directly on it or builds from it, you'll be gaining, good luck.. PS always double check that the size of your char arrays, you don't people exploding your buffffer smile.gif

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this