[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] bind()




"Erwin S. Andreasen" wrote:

> On Wed, 26 Jan 2000, Frederik Treue wrote:
>
> > Jeg sidder og leger med mine første netværks programmer, og er løbet ind
> > i problemer med bind() functionen. Jeg vil til testformål gerne kunne
> > binde min socket til lo, men jeg får at vide at "address already in
> > use"??? Jeg har sat SO_REUSEADDR på den socket jeg forsøger med? Nogen
> > der kan komme med forslag til hvad jeg gør galt?
>
> Linie 14 og 15 i din kode ser suspekte ud.
>
> Hint, hint ;)
>
> --
> ==============================================================================
> Erwin Andreasen   Herlev, Denmark <sslug@sslug>          UNIX System Programmer
> <URL:http://www.andreasen.org>              <*>         (not speaking for) DDE
> ==============================================================================

    Hmm, utroligt så meget man kan finde ud af ved at skulle forklare sin kode for
andre mennesker. Det viste sig at jeg ikke havde fået lukket min fd til lo,
hvorefter den selvfølgelig brokkede sig.

    Nu har jeg til gengæld et andet problem. Når jeg forsøger at close() får jeg
en seqfault, uanset hvad jeg beder den om at close()?


server program:
/************************************************
 * server - a very simpel network server. *
 * Originally written by : Frederik Treue       *
 * aka Dr. Strangelove <sslug@sslug> *
 * Purpose : to become a unix network coder :) *
 ************************************************/

#include <iostream>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
using namespace std;
#define MYPORT 3490  // arbitrary port
#define BACKLOG 10  // max number of users on port

void main()
  {
  int len = 20;  // max length of recieved data
  int bytes_recv;  // number of bytes recv()'ed / error message
  int sock_fd;  // socket to listen() on
  int new_fd;  // socket to accept()
  struct sockaddr_in my_addr;  // info on my socket
  struct sockaddr_in their_addr;  // info of senders socket
  socklen_t sin_size;  // length of struct sockaddr_in
  char *buf;  // buffer to hold data
  int i=1;  // integer used by setsockopt()

  // first, allocate the socket to the listen operation
  sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fd==-1)  // error check
    {
    cerr << "listen socket allocation error : " << endl;
    cerr << strerror(errno) << endl;
    exit(1);
    }

  // then fill apropriate data into my_addr
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(MYPORT);
  my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  bzero(&(my_addr.sin_zero), 8);

  // set listen socket to accept more sockets on address (?)
  setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));

  // now, try binding the listen socket to the loopback interface
  cout << "my_addr : " << my_addr.sin_family << " : " << my_addr.sin_port << " : "
<< my_addr.sin_addr.s_addr << endl;  // debug info
  int err=bind(sock_fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
  cout << err << endl;
  if (err==-1)  // error check
    {
    cerr << "bind failure" << endl;
    cerr << strerror(errno) << endl;
    exit(1);
    }

  // start listening on listen socket
  if (listen (sock_fd, BACKLOG))  // error check
    {
    cerr << "Error listening to port" << endl;
    cerr << strerror(errno) << endl;
    exit(1);
    }

  sin_size = sizeof(struct sockaddr_in);  // find size of struct sockaddr_in
  // attempt to accept incoming signal
  new_fd = accept(sock_fd, (struct sockaddr *)&their_addr, &sin_size);
  if (new_fd == -1)  // error check
    {
    cerr << "error accepting connection" << endl;
    cerr << strerror(errno) << endl;
    exit (1);
    }

  // attempt to recv() incoming signal
  bytes_recv = recv(new_fd, buf, len, 0);

  // end the received signal
  *(buf+bytes_recv) = '\0';

  // print out length of recieved signal and the actual signal
  cout << bytes_recv << endl;
  cout << endl << buf << endl;

  cerr << close (new_fd) << endl;  // these cause a
  cerr << close (sock_fd) << endl;  // segfault, no matter what args!

  }

client program :
#include <string.h>
#include <iostream>
#include <arpa/inet.h>
#include <errno.h>
using namespace std;

#define DEST_IP "127.0.0.1"  // the address to send to
#define DEST_PORT 3490  // arbitrary port

void main()
  {
  int sockfd;  // fd for socket to send() to
  struct sockaddr_in dest_addr;  // the struct sockaddr_in holding destination
  char *msg = "Kukkeluk!";  // the message to be send()
  int len;  // length of message to be send()
  int bytes_sent;  // the number of bytes actually send()

  // first, define the fd for the socket to be used for send()ing
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd==-1)  // error check
    {
    cout << "socket() returned -1" << endl;
    cout << strerror(errno);
    exit(1);
    }

  // fill appropriate info into dest_addr
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(DEST_PORT);
  if (!inet_aton(DEST_IP,&dest_addr.sin_addr))  // errorcheck inet address
    {
    cout << "failure assigning destination IP" << endl;
    exit(1);
    }
  memset(&(dest_addr.sin_zero),0,8);

  // make connection to detination, using defined socket
  connect(sockfd, (struct sockaddr *) &dest_addr, sizeof(sockaddr));
  if (errno)  // error check
    {
    cout << "Error connecting : " << endl;
    cout << strerror(errno) << endl;
    exit(1);
    }

  len = strlen(msg);  // find length of message to be send()
  bytes_sent = send(sockfd, msg, len, 0);  // attempt to send the message
  }

der er også en attachment med de to filer med denne post


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *