Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* basic socket operations, UNIX-native */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <stdlib.h> // for exit
- # include <sys/types.h>
- # include <sys/socket.h>
- # include <netinet/in.h>
- # include <netdb.h>
- # define SIZE 1024
- # define exception(x) { puts(x); exit(1); }
- int main(int argc, char * argv[])
- {
- /* recvfrom */
- int sock;
- struct sockaddr_in mysock;
- struct sockaddr addr;
- int addrlen;
- char buffer[SIZE]; // stores data
- sock = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket
- mysock.sin_family = AF_INET;
- mysock.sin_port = 1999;
- mysock.sin_addr.s_addr = INADDR_ANY;
- bind(sock, &mysock, sizeof(mysock)); // binding sock and mysock
- // COMMENTED LINE BELOW causes infinite loop, why?
- //if(recvfrom(sock, buffer, SIZE, 0, &addr, &addrlen) == -1) // done receiving condition
- exception("Call error in recvfrom(...)\n");
- /* sendto */
- /* SOCK, MYSOCK and BUFFER will be used without being reinstanced */
- int success;
- struct hostent * hp;
- hp = gethostbyname("dis.uniroma1.it");
- memcpy(&mysock.sin_addr, hp->h_addr, 4);
- success = sendto(sock, buffer, SIZE, 0, &mysock, sizeof(mysock));
- if(success == -1) exception("Call error in sendto(...)\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement