socket structure in c

28
2: Application Layer 1 Socket structure in C struct sockaddr_in { //Address family, always AF_INET short int sin_family; // Port number unsigned short int sin_port; // struct for IP address struct in_addr sin_addr; // A part that is always zero unsigned char sin_zero[8]; }; struct in_addr { // IP address 32-bit unsigned long s_addr; };

Upload: dolph

Post on 13-Feb-2016

23 views

Category:

Documents


1 download

DESCRIPTION

Socket structure in C. struct sockaddr_in { //Address family, always AF_INET short int sin_family; // Port number unsigned short int sin_port; // struct for IP address struct in_addr sin_addr; // A part that is always zero unsigned char sin_zero[8]; }; . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Socket structure in C

2: Application Layer 1

Socket structure in Cstruct sockaddr_in {

//Address family, always AF_INETshort int sin_family; // Port number unsigned short int sin_port; // struct for IP address struct in_addr sin_addr; // A part that is always zerounsigned char sin_zero[8]; };

struct in_addr { // IP address 32-bit unsigned long s_addr; };

Page 2: Socket structure in C

2: Application Layer 2

Host and Network Byte OrderNetwork byte order is “most significant byte first”1.2.3.4 stored as Host byte order could be either “most significant byte first” or “least significant byte first” 1.2.3.4 stored as There are functions to convert a number between host and network byte order:long htonl(long)short htons(short)long ntohl(long)short ntohs(short)

1 2 3 4

4 3 2 1

Page 3: Socket structure in C

2: Application Layer 3

Socket structure in Cstruct sockaddr_in {

//Address family, always AF_INETshort int sin_family; // Port number unsigned short int sin_port; // struct for IP address struct in_addr sin_addr; // A part that is always zerounsigned char sin_zero[8]; };

network order

network order

Sockets are referenced via a socket descriptor which is of type intThere is also a struct called sockaddr and some functions need that datatype as an argument

Page 4: Socket structure in C

2: Application Layer 4

IP address (32-bit string)To obtain a 32-bit address in network orderfrom a string long inet_addr(“1.2.3.4”)To obtain a dotted-notation string from the address in 32-bitchar* inet_ntoa(struct in_addr)

Page 5: Socket structure in C

2: Application Layer 5

Using DNSstruct hostent

{ char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list;

};

//Getting yahoo’s IP addressstruct hostent h;struct in_addr in;//Call DNSh=gethostbyname(“www.yahoo.com”);//Create address struct so we can call inet_ntoain.s_addr = h->haddr;printf(“Address is %s\n”, inet_ntoa(in));

First element is called haddr

Page 6: Socket structure in C

2: Application Layer 6

C Socket programming using TCPExample client-server

app:1) client gets a line from the

server and prints it out

client server

1. waiting on a port2. connect

3. line

Page 7: Socket structure in C

2: Application Layer 7

C client using TCP#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h>

// the port client will be connecting to #define PORT 3490 // max number of bytes we can get at once #define MAXDATASIZE 100

Page 8: Socket structure in C

2: Application Layer 8

C client using TCPint main(int argc, char *argv[]) {

int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he;

// server's addressstruct sockaddr_in server_addr;

//error checkingif (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); }

Page 9: Socket structure in C

2: Application Layer 9

C client using TCP// get the host info via DNSif ((he=gethostbyname(argv[1])) == NULL) {

perror("gethostbyname"); exit(1);

} // create the socket, this will not automatically connect itif ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("socket"); exit(1);

}

Page 10: Socket structure in C

2: Application Layer 10

C client using TCP// fill in server dataserver_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(server_addr.sin_zero), '\0', 8);

// connect to server (handshaking)if (connect(sockfd, (struct sockaddr *)&server_addr,

sizeof(struct sockaddr)) == -1) {

perror("connect"); exit(1);

}

Page 11: Socket structure in C

2: Application Layer 11

C client using TCP// receive data from serverif ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); }

// put the end-of-string character and print it outbuf[numbytes] = '\0'; printf("Received: %s",buf);

// close socket and exitclose(sockfd); return 0;

}

Page 12: Socket structure in C

2: Application Layer 12

C server using TCP#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/wait.h> #include <signal.h>

// the port users will be connecting to #define MYPORT 3490// how many pending connections queue will hold #define BACKLOG 10

Page 13: Socket structure in C

2: Application Layer 13

C server using TCPint main(void) {

int welcomesock, connectionsock; // my address information struct sockaddr_in my_addr; // client address information struct sockaddr_in client_addr; socklen_t sin_size; int yes=1;

// create socket for waitingif ((welcomesock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); }

Page 14: Socket structure in C

2: Application Layer 14

C server using TCP// sockets usually take some time to be released by OS// doing this ensures that you can re-run your server right // after it exits, on the same portif(setsockopt(welcomesock,SOL_SOCKET,SO_REUSEADDR,

&yes,sizeof(int)) == -1) {

perror("setsockopt"); exit(1);

}

// fill in my address datamy_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); // automatically fill IP address of this machinemy_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '\0', 8); // zero the rest

Page 15: Socket structure in C

2: Application Layer 15

C server using TCP// associate welcome socket with portif (bind(welcomesock, (struct sockaddr *)&my_addr,

sizeof(struct sockaddr)) == -1) {

perror("bind"); exit(1);

}

// start waiting for clientsif (listen(welcomesock, BACKLOG) == -1) {

perror("listen"); exit(1);

}

Page 16: Socket structure in C

2: Application Layer 16

C server using TCP// loop foreverwhile(1) {

sin_size = sizeof(struct sockaddr_in);

// accept client’s connection if ((clientsock = accept(welcomesock,

(struct sockaddr *)&client_addr, &sin_size)) == -1) { perror("accept"); continue; }

// print a message about new connectionprintf("server: got connection from %s\n", inet_ntoa(client_addr.sin_addr));

Page 17: Socket structure in C

2: Application Layer 17

C server using TCP// send server’s messageif (send(clientsock, "Hello, world!\n", 14, 0) == -1){ perror("send"); close(clientsock); exit(0); } // close this connection with the clientclose(clientsock); } //end of loop forever

//close welcome socket and exitclose(welcomesock); return 0;

}

Page 18: Socket structure in C

2: Application Layer 18

C Socket programming using UDPExample client-server app:1) client sends a line to the server

client server

1. waiting on a port2. line

Page 19: Socket structure in C

2: Application Layer 19

C client using UDP#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h>

// the port client will be connecting to #define PORT 3490 // max number of bytes we can get at once #define MAXDATASIZE 100

Page 20: Socket structure in C

2: Application Layer 20

C client using UDPint main(int argc, char *argv[]) {

int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he;

// server's addressstruct sockaddr_in server_addr;

//error checkingif (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); }

Page 21: Socket structure in C

2: Application Layer 21

C client using UDP// get the host info via DNSif ((he=gethostbyname(argv[1])) == NULL) {

perror("gethostbyname"); exit(1);

} // create the socketif ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {

perror("socket"); exit(1);

}

Page 22: Socket structure in C

2: Application Layer 22

C client using UDP// fill in server dataserver_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); server_addr.sin_addr = *((struct in_addr *)he->h_addr);

memset(&(server_addr.sin_zero), '\0', 8);

// send message to serverif ((numbytes=sendto(sockfd, “Hi\n”, 3, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } // close socket and exitclose(sockfd);return 0;

}

Page 23: Socket structure in C

2: Application Layer 23

C server using UDP#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/wait.h> #include <signal.h>

// the port users will be connecting to #define MYPORT 3490

Page 24: Socket structure in C

2: Application Layer 24

C server using UDPint main(void) {

int welcomesock; // my address information struct sockaddr_in my_addr; // client address information struct sockaddr_in client_addr; socklen_t sin_size; int yes=1;

// create socket for waitingif ((welcomesock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); }

Page 25: Socket structure in C

2: Application Layer 25

C server using UDP// sockets usually take some time to be released by OS// doing this ensures that you can re-run your server right // after it exits, on the same portif(setsockopt(welcomesock,SOL_SOCKET,SO_REUSEADDR,

&yes,sizeof(int)) == -1) {

perror("setsockopt"); exit(1);

}

// fill in my address datamy_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); // automatically fill IP address of this machinemy_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), '\0', 8); // zero the rest

Page 26: Socket structure in C

2: Application Layer 26

C server using UDP// associate welcome socket with portif (bind(welcomesock, (struct sockaddr *)&my_addr,

sizeof(struct sockaddr)) == -1) {

perror("bind"); exit(1);

}

Page 27: Socket structure in C

2: Application Layer 27

C server using UDP// loop foreverwhile(1) {

addr_len = sizeof(struct sockaddr);

// get a packet, no listen or acceptif ((numbytes = recvfrom(welcomesock, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&client_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); }

// print a message about clientprintf("server: got message from %s\n", inet_ntoa(client_addr.sin_addr));

Page 28: Socket structure in C

2: Application Layer 28

C server using UDPprintf("packet is %d bytes long\n",numbytes); buf[numbytes] = '\0'; printf("packet contains \"%s\"\n",buf);

}

} //end of loop forever

// close welcome socket and exitclose(welcomesock);return 0;

}