April 1st, 2003, 10:47 AM
Socket Programming Part 1
i don't see much of tuts on programming .this tut is on socket programming.
i have used a book of richerd stevens to study this subject.it is wonderdful
it is just an introduction to socket programming which would help anyone interested
in learning socket programming.it ijs just the beginning of socket programming i would certainly post another one if this post is appriciated.pleaase let me know what would like my next tut to include.
sockets can be connection based and connectionless.each socket has an
associated type that determine the manner in which data is transmitted
between 2 sockets.
1)virtual circuit :data aretransmitted sequencially in a realiable
2)datagram:data are transmitted nonsequencially in a reliable fashion.
these are generally fasterthen virtual circuit.
socket thatcommunicate may be of same or different types.
FUNCTIONS USED IN OCKET PROGRAMMING:
creates sockets of a given domain,type,protocol.
int socket(int domain,int type,int proto);
domain argument specifies socket naming convention and proto address
example values generally used in programming are AF_INET,AF_UNIX
type argument specifies a socket type these are
protocol argument :
~ specifies a perticular proto to hich socket use!generally it is assigned zero!
return value is an int alue which is -1 on failure and any other integer on success
this function assigns a name to socket
int bind(int sid,struct sockaddr* add, int len);
first argument is socket desceriptor (value returned from socket function),second arg is
socket address struct it is different for different domains!
third argument is length of object pointed by second add!
retunrs 0 on success and -1 on failure!
this function is used in server process to establish connection based socket of type SOCK_TREAM
again sid argument which has been explained above.
anather argument is used in it to specify number of connection request that may be queued for
return value is 0 on success and 1 on failure.
int connect(int sid,struct sockaddr*add,int len);
this is called in a client process to request a connection!
int accept(int sid,struct sockaddr* add,int len);
called in a server process to establish a connection based socket
connection with the client!
int send(int sid,const char* buf,int len,int flag);
it sends a mesage contained in buf of size len to a connected socket!
flag argument is usually zero.try some other values for experiment.
int recv(int sid,char* buf,int len,int flag);
yeah i know u are al;l smart enough tol understand what this function is doing but for those
who can't it is receiving data and coping it in buf through a socket designated by sid.
well now that u know basic functions used in socket programming make ur own client server application with the helpof book i refered above.
to comlile a c program in RH 8.0 type cc file.c and if it ia a cpp program c++ c++ file.cpp
u can run ur client server application on ur pc itself to do o type /a.out 127.0.0.1
April 1st, 2003, 12:25 PM
cool this is what i was looking for -networking plz add some advance topic in next tut
April 1st, 2003, 12:29 PM
well i would also love it if u give some nice tut on advanced topic but for starters it is good to get introduced .
and by the way is it true that hacking tools like BO2k use socket programming?
April 1st, 2003, 12:44 PM
well yes it is true BO2k uses socket programming it is made in VC++ i don't wanna say anything more there are ppl here who are really desparate to get me banned
April 1st, 2003, 04:00 PM
thatnks ive been looking for somthing like this.
Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"
April 1st, 2003, 09:09 PM
I actually just finished up an assignment on socket programming for my operating systems class. It's usually a good thing to have an example to look at, so here it is.
We had to write a client which read a line from standard in, sent it to a server and if the server repiled "OK" it would exit smoothly, otherwise it would print an error message.
The server accepted a client connection, forked a child process to handle the client, read in the data from the socket, printed it to standard out and sent "OK" to the client.
Here's the code. I commented it pretty well so hopefully you can trace through it.
Based on a suggestion, I want to point a few things out. This is program was written in C on a Linux machine. It will NOT run of Windows, because it uses the *nix system call fork() which creates a new process on *nix machines only. Hopefully this clears a few things up!
April 2nd, 2003, 10:01 AM
thanks for the file algaen i think it would help all those who are just starting socket programming .
i would suggest each newbie should go through lines of that program and try to figure out what it is doing.
like--line-- s = socket(AF_INET, SOCK_STREAM, 0);
what is this line doing it is creating of domain AF_INET and type SOCK_STREAM and so on .the value returned from it (s) is an integer which will be used through out program (it is called socket descriptor).i will certainly post a few more tuts on socket programming soon just wait .....
April 2nd, 2003, 10:37 AM
Mmmm, gonna get me a socket programming book after ive finished my c book.
Ive found that online tutorials are usually to specific, and books teach a really good broad knowledge base that you can build on.
A good tut thou, will wait for number 2
April 3rd, 2003, 11:34 AM
i would like to add an online link to socket programming it might be useful to some of you!