OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [udp/] [udp_lite.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
// udp_lite.c - LM
2
 
3
 
4
 
5
 
6
#include <stdio.h>      /* for printf() and fprintf() */
7
#include <sys/socket.h> /* for socket() and bind() */
8
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
9
#include <stdlib.h>     /* for atoi() and exit() */
10
#include <string.h>     /* for memset() */
11
#include <unistd.h>     /* for close() */
12
 
13
#define ServPort 6000
14
 
15
 
16
int sock;                        /* Socket */
17
struct sockaddr_in ServAddr; /* Local address */
18
struct sockaddr_in ClntAddr; /* Client address */
19
unsigned int cliAddrLen;         /* Length of incoming message */
20
char Buffer[maxBuffer];        /* Buffer for echo string */
21
unsigned short ServPort;     /* Server port */
22
int recvMsgSize;                 /* Size of received message */
23
 
24
void create_socket()
25
{
26
 
27
 
28
 
29
    /* Create socket for sending/receiving datagrams */
30
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
31
        DieWithError("socket() failed");
32
 
33
    /* Construct local address structure */
34
    memset(&ServAddr, 0, sizeof(ServAddr));   /* Zero out structure */
35
    ServAddr.sin_family = AF_INET;                /* Internet address family */
36
    ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
37
    ServAddr.sin_port = htons(ServPort);      /* Local port */
38
 
39
    /* Bind to the local address */
40
    if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
41
        DieWithError("bind() failed");
42
 
43
}
44
 
45
 
46
 
47
void udp_send(buffer, buffer_size)
48
{
49
 
50
 
51
   /* Send datagrams to the client */
52
    if (sendto(sock, buffer, buffer_size, 0,
53
               (struct sockaddr *) &ClntAddr, sizeof(ClntAddr)) != MsgSize)
54
            DieWithError("sendto() sent a different number of bytes than expected");
55
    }
56
    /* NOT REACHED */
57
}
58
 
59
void udp_recv(){
60
 
61
  /* Block until receive message from a client */
62
  if ((recvMsgSize = recvfrom(sock, Buffer, maxBuffer, 0,
63
  (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
64
    DieWithError("recvfrom() failed");
65
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.