Download presentation
Presentation is loading. Please wait.
1
Part 2 Socket Programming UDP
2
Socket programming with UDP
UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet UDP: transmitted data may be received out of order, or lost application viewpoint: UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Application 2-2
3
Client/Server Socket Interaction: UDP
No handshaking. No streams are attached to the sockets. Sender creates packets of bytes with destination IP address and Port number. Receiving host must unravel each packet received to obtain the packet’s information bytes.
4
Client/server socket interaction: UDP
Server (running on hostid) create socket, Client Create datagram with server (hostid, port=x), send datagram via clientSocket create socket, port= x. clientSocket = socket(PF_INET, SOCK_DGRAM,0) serverSocket = socket(PF_INET, SOCK_DGRAM,0) read datagram from serverSocket close clientSocket read datagram from write reply to serverSocket specifying client address, port number Application 2-4
5
Example: UDP client Client process client UDP socket
Input: receives packet (recall that TCP receives a “byte stream”) Output: sends packet (recall that TCP sends a “byte stream”) client UDP socket Application 2-5
6
Functions and Data Structures
7
sendto() Send data through a socket:
sendto(SOCKET s, char *msg, int msglen, int flags, struct sockaddr *to, int *tolen); PARAMETERS s = socket (inside the socket descriptor: port and IP address...) msg = a pointer to a buffer (could be a string) msglen = the length of the buffer flags = 0 (forget about them for this exercise...) to=structure of address with the IP / port # tolen=length of the structure The WSAStartup function is called to initiate use of WS2_32.dll. Every Winsock application must load the appropriate version of the Winsock DLL. If you fail to load the Winsock library before calling a Winsock function, the function will return a SOCKET_ERROR and the error will be WSANOTINITIALISED . Loading the Winsock library is accomplished by calling the WSAStartup function WSVERS The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number. Example: sendto(s, sbuffer, strlen(sbuffer),0,(struct sockaddr*) to, &len);
8
recvfrom() Receive data int recvfrom(SOCKET s, char *msg, int msglen,
int flags, struct sockaddr *from, int *fromlen) PARAMETERS s = socket (inside the socket descriptor: port and IP address...) msg = a pointer to a buffer msglen = the length of the buffer flags = 0 from =structure of address with the IP / port # fromlen=length of the structure The WSAStartup function is called to initiate use of WS2_32.dll. Every Winsock application must load the appropriate version of the Winsock DLL. If you fail to load the Winsock library before calling a Winsock function, the function will return a SOCKET_ERROR and the error will be WSANOTINITIALISED . Loading the Winsock library is accomplished by calling the WSAStartup function WSVERS The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number. Example: recvfrom(s, rbuffer, 1, 0,(struct sockaddr *) &from, &len);
9
Demo
10
Test the UDP Client-Server Codes
Run the server: ServerUDP 1235 Compile ClientUDP.c, look for the executable. Run ClientUDP.exe from the command prompt to connect to the server: ClientUDP localhost 1235 or ClientUDP Alternatively, use IpConfig to find out what your IP address is: (e.g ), then connect to the server using: ClientUDP
11
Source Codes
12
Client UDP clientUDP.cpp #include <stdio.h> //In LINUX
#include <string.h> #include <stdlib.h> #include <winsock2.h> #define WSVERS MAKEWORD(2,0) WSADATA wsadata; #define BUFFERSIZE 80 #define SEGMENTSIZE 78 void get_keyboard(char * send_buffer) { fgets(send_buffer,SEGMENTSIZE,stdin); if (send_buffer[strlen(send_buffer)-1]=='\n') { //if the last one is \n, which means there is at least one ‘\0’ after it send_buffer[strlen(send_buffer)-1]='\r'; send_buffer[strlen(send_buffer)]='\n'; } else { send_buffer[strlen(send_buffer)]='\n'; //write \n on the last byte clientUDP.cpp //In LINUX #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>
13
Client UDP clientUDP.cpp int main(int argc, char *argv[]) {
//******************************************************************* // Initialization struct sockaddr_in localaddr,remoteaddr; memset(&localaddr, 0, sizeof(localaddr));//clean up memset(&remoteaddr, 0, sizeof(remoteaddr));//clean up //******************************************************************** // WSSTARTUP if (WSAStartup(WSVERS, &wsadata) != 0) { WSACleanup(); printf("WSAStartup failed\n"); } int s; char send_buffer[BUFFERSIZE],receive_buffer[BUFFERSIZE]; int n,bytes; if (argc == 1) { printf("USAGE: client IP-address [port]\n"); exit(1); //Port number: get it from argv[2], convert/copy to sin_port if (argc == 3) remoteaddr.sin_port = htons((u_short)atoi(argv[2])); else remoteaddr.sin_port = htons(1234); //Family of protocols remoteaddr.sin_family = AF_INET; clientUDP.cpp The WSAStartup function is called to initiate use of WS2_32.dll. Every Winsock application must load the appropriate version of the Winsock DLL. If you fail to load the Winsock library before calling a Winsock function, the function will return a SOCKET_ERROR and the error will be WSANOTINITIALISED . Loading the Winsock library is accomplished by calling the WSAStartup function WSVERS The high order byte specifies the minor version (revision) number; the low-order byte specifies the major version number.
14
Client UDP clientUDP.cpp
//******************************************************************* //GETHOSTBYNAME struct hostent *h; if ((h=gethostbyname(argv[1])) != NULL) { memcpy(&remoteaddr.sin_addr,h->h_addr,h->h_length); //get remote IP address } else if ((remoteaddr.sin_addr.s_addr = inet_addr(argv[1])) == INADDR_NONE) { printf("An error occured when trying to translate to IP address\n"); exit(1); } //CREATE CLIENT'S SOCKET s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { printf("socket failed\n"); printf("\nSOCKET created.\n"); memset(send_buffer, 0, sizeof(send_buffer));//clean up printf("\nwaiting for user input...\n"); clientUDP.cpp
15
Client UDP clientUDP.cpp get_keyboard(send_buffer);
while (strncmp(send_buffer,".",1) != 0) { //******************************************************************* //SEND bytes = sendto(s, send_buffer, strlen(send_buffer),0, (struct sockaddr *)(&remoteaddr),sizeof(remoteaddr) ); printf(“TO SENDER: %s \n",send_buffer); if (bytes < 0) { printf("send failed\n"); exit(1); } memset(send_buffer, 0, sizeof(send_buffer));//clean up closesocket(s); //close(s); //in Linux return 0; clientUDP.cpp
16
Server UDP serverUDP.cpp #include <errno.h>
#include <stdio.h> #include <string.h> #include <winsock2.h> #define WSVERS MAKEWORD(2,0) WSADATA wsadata; #ifndef INET_ADDRSTRLEN #define INET_ADDRSTRLEN 16 #endif int main(int argc, char *argv[]) { //******************************************************************** // INITIALIZATION struct sockaddr_in localaddr,remoteaddr; int s; char send_buffer[80],receive_buffer[80]; char remoteIP[INET_ADDRSTRLEN]; int remotePort=1234; int localPort;//no need for local IP... int n,bytes,addrlen; memset(&localaddr,0,sizeof(localaddr));//clean up the structure memset(&remoteaddr,0,sizeof(remoteaddr));//clean up the structure serverUDP.cpp
17
Server UDP serverUDP.cpp
//******************************************************************** // WSSTARTUP if (WSAStartup(WSVERS, &wsadata) != 0) { WSACleanup(); printf("WSAStartup failed\n"); } //SOCKET s = socket(PF_INET, SOCK_DGRAM, 0); if (s <0) { printf("socket failed\n"); localaddr.sin_family = AF_INET; if (argc == 2) localaddr.sin_port = htons((u_short)atoi(argv[1])); else localaddr.sin_port = htons(1235);//default port localaddr.sin_addr.s_addr = INADDR_ANY;//server address should be local //BIND (notice, no listen()...)? if (bind(s,(struct sockaddr *)(&localaddr),sizeof(localaddr)) != 0) { printf("Bind failed!\n"); return 1; serverUDP.cpp
18
Server UDP serverUDP.cpp //INFINITE LOOP
//******************************************************************** while (1) { addrlen = sizeof(struct sockaddr_in); //RECEIVE printf("Waiting... \n"); bytes = recvfrom(s, receive_buffer, 78, 0,(struct sockaddr *)(&remoteaddr),&addrlen); printf("Received %d bytes\n",bytes); //PROCESS REQUEST n=0; while (n<bytes){ n++; if ((bytes < 0) || (bytes == 0)) break; if (receive_buffer[n] == '\n') { /*end on a LF*/ receive_buffer[n] = '\0'; break; } if (receive_buffer[n] == '\r') /*ignore CRs*/ printf("After processing, recvbuffer is %s \n",receive_buffer); closesocket(s); return 0; serverUDP.cpp
19
EXTRA
20
fgets function char * fgets ( char * str, int num, FILE * stream );
<cstdio> char * fgets ( char * str, int num, FILE * stream ); Get string from stream Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string. Parameters str Pointer to an array of chars where the string read is stored. num Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used. stream Pointer to a FILE object that identifies the stream where characters are read from. To read from the standard input, stdin can be used for this parameter.
21
memset function void * memset ( void * ptr, int value, size_t num );
<cstring> void * memset ( void * ptr, int value, size_t num ); Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char). Parameters ptr Pointer to the block of memory to fill. value Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value. num Number of bytes to be set to the value.
22
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.