|
-
April 1st, 2003, 10:47 AM
#1
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.
SOCKET:
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.
SOCKET TYPES:
1)virtual circuit :data aretransmitted sequencially in a realiable
fashion.
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:
SOCKET:
creates sockets of a given domain,type,protocol.
#include<sys/socket.h>
#include<sys/types.h>
int socket(int domain,int type,int proto);
where
domain argument specifies socket naming convention and proto address
format!
example values generally used in programming are AF_INET,AF_UNIX
type argument specifies a socket type these are
a>SOCK_STREAM
b>SOCK_DGRAM
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
BIND:
this function assigns a name to socket
#include<sys/socket.h>
#include<sys/types.h>
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!
LISTEN:
this function is used in server process to establish connection based socket of type SOCK_TREAM
or SOCK_SEQPACKET
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
the socket!
return value is 0 on success and 1 on failure.
connect:
#include<sys/socket.h>
#include<sys/types.h>
int connect(int sid,struct sockaddr*add,int len);
this is called in a client process to request a connection!
ACCEPT:
#include<sys/socket.h>
#include<sys/types.h>
int accept(int sid,struct sockaddr* add,int len);
called in a server process to establish a connection based socket
connection with the client!
SEND:
#include<sys/socket.h>
#include<sys/types.h>
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.
RECV:
#include<sys/socket.h>
#include<sys/types.h>
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|