Results 1 to 9 of 9

Thread: Socket Programming Part 1

  1. #1

    Thumbs up 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

  2. #2
    cool this is what i was looking for -networking plz add some advance topic in next tut

  3. #3
    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?

  4. #4
    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

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    186
    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.\"

  6. #6
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    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!

  7. #7
    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 .....

  8. #8
    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

  9. #9
    i would like to add an online link to socket programming it might be useful to some of you!
    http://www.ecst.csuchico.edu/~beej/guide/net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •