This is an old revision of the document!


Vortrag/Workshop Einführung in Einführung Unix Socket Programmierung

  • by Christian Pointner

Beispiel: udpclient

/*
 *  udpclient
 *
 *  This is a very basic UDP client example to show the usage of
 *  sockets. 
 *  
 *  Copyright (C) 2010 Christian Pointner <equinox@realraum.at>
 *
 *  udpclient is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  udpclient is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with udpclient. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <arpa/inet.h>

int main(int argc, char** argv)
{
  if(argc <= 2) {
    fprintf(stderr,"too few arguments\n");
    exit(1);
  }
  printf("starting UDP Client\n");

  int sock = socket(AF_INET, SOCK_DGRAM, 0);
  if(sock < 0) { 
    perror("socket() call failed");
    exit(-1);
  }
    
  struct sockaddr_in local_addr;
  memset((char *) &local_addr, 0, sizeof(local_addr));
  local_addr.sin_family = AF_INET;
  local_addr.sin_port = htons(4321);
  local_addr.sin_addr.s_addr = htonl(INADDR_ANY);

  if(bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr))==-1) { 
    perror("bind() call failed");
    exit(-1);
  }

  struct sockaddr_in remote_addr;
  int alen, len;
  alen = sizeof(remote_addr);
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons(atoi(argv[2]));
  if (inet_aton(argv[1], &remote_addr.sin_addr)==0) {
    perror("inet_aton() call failed");
    exit(-1);
  }

  uint8_t buffer[1500];
  for (;;) {
    if((len = read(0, buffer, 1500))==-1) {
      perror("read() call failed");
      exit(-1);
    }

    if((len = sendto(sock, buffer, len, 0, (struct sockaddr *)&remote_addr, alen))==-1) {
      perror("sendto() call failed");
      exit(-1);
    }
  }

  close(sock);
  
  printf("exiting UDP Client\n");

  return 0;
}

/* * udpserver * * This is a very basic UDP client example to show the usage of * sockets. * * Copyright (C) 2010 Christian Pointner equinox@realraum.at * * udpserver is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * udpserver is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with udpserver. If not, see <http://www.gnu.org/licenses/>. */

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h>

#include <sys/types.h> #include <sys/socket.h>

#include <arpa/inet.h>

int main(int argc, char** argv) {

printf("starting UDP Server\n");
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) { 
  perror("socket() call failed");
  exit(-1);
}
  
struct sockaddr_in local_addr;
memset((char *) &local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(1234);
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr))==-1) { 
  perror("bind() call failed");
  exit(-1);
}
struct sockaddr_in remote_addr;
int alen, len;
alen = sizeof(remote_addr);
uint8_t buffer[1500];
for (;;) {
  if((len = recvfrom(sock, buffer, 1500, 0, (struct sockaddr *)&remote_addr, &alen))==-1) {
    perror("recvfrom() call failed");
    exit(-1);
  }
  printf("Received %d Bytes from %s:%d\n", len, inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
}
close(sock);

printf("exiting UDP Server\n");
return 0;

} </file>

Beispiel: udpserver

<file>


realraum Graz, Brockmanngasse 15, 8010 Graz, realraum - Verein für Technik in Kultur und Gesellschaft
  • /var/lib/dokuwiki/data/attic/workshop/sockets.1285782613.txt.gz
  • Last modified: 2010-09-29 19:50
  • by equinox