Download presentation
Presentation is loading. Please wait.
1
Sockets
2
What is Socket? The end point of communication between 2 systems on a network (combination of IP&port on each end of the communication) 4-Tuple Name of API
3
http://inst. eecs. berkeley
4
Socket in C Stream Socket or Connection Oriented Socket
Header files used #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h>
5
Socket: Server side int sockt, newsockt; … sockt = socket(AF_INET, SOCK_STREAM, 0); Address Family: AF_INET or AF_UNIX Socket Type: SOCK_STREAM or SOCK_DGRAM Protocol: 0 for appropriate protocol selected by OS
6
If sockt > 0 Socket openning successful
7
struct sockaddr_in s_addr, c_addr; … struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; } AF_INET Not use
8
Initialize sockaddr_in
bzero((char *) &s_addr, sizeof(s_addr)); bzero((char *) &c_addr, sizeof(c_addr));
9
Set sockaddr_in Must be s_addr.sin_family = AF_INET; s_addr.sin_port = htons(2048); s_addr.sin_addr.s_addr = INADDR_ANY Constant or Symbolic link Represent host’s IP address Convert to Networks Byte Order
10
Binding socket bind(sockt, (struct sockaddr *) &s_addr, sizeof(s_addr)); bind returns 0 on success returns -1 in case of fail
11
Waiting for connection
listen(sockt, 5); Size of backlog queue Socket to be used for listening
12
Accept a remote connection
int caddr_len = sizeof(c_addr); … newsockt = accept(sockt, (struct sockaddr *) &c_addr, &caddr_len); accept() blocks the process Returns -1 in case of fail Returns non-negative in case of success
13
Receive data via opened socket
char buffer[256]; int n; … bzero(buffer, 256); n = read(newsockt, buffer, 255); read() blocks the process until there is something to read read() returns number of bytes read read() reads 255 bytes or less
14
Send data via opened socket
int n; … n = write(newsockt, “Test 1234”, 9); - write() returns number of sent bytes
15
Close Socket close(newsockt); close(sockt);
16
Socket: Client side Library Ip address of remote host sys/types.h
sys/socket.h netinet/in.h netdb.h or arpa/inet.h Ip address of remote host Is known -> inet_pton() is define in this lib. Structure hostent and gethostbyname() function are defined in this library
17
struct hostent. remoteServ … struct hostent { char. h_name; char
struct hostent *remoteServ … struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; #define h_addr h_addr_list[0] }; Host name Alias name list Host address type Address length Address list from name server For backward compatibity
18
Get remote host by name struct sockaddr_in sock_serv; … remoteServ = gethostbyname(“ bzero((char *) &sock_serv, sizeof(sock_serv)); sock_serv.sin_family = AF_INET; bcopy((char *)remoteServ->h_addr, (char *) &sock_serv.sin_addr.s_addr, remoteServ->h_length); sock_serv.sin_port = htons(2048);
19
gethostbyname() return null if fail
20
IP of remote host is known
inet_pton(int af, char *src, void *dst); Address family: AF_INET struct in_addr Or converted IP IP address in string form inet_pton returns 1 on success, returns 0 on failure
21
sock_serv. sin_family = AF_INET; sock_serv
sock_serv.sin_family = AF_INET; sock_serv.sin_port = htons(2048); Inet_pton(AF_INET, “ ”, &sock_serv.sin_addr);
22
Connect to remote host int sockHandle; … connect(sockHandle, &sock_serv, sizeof(sock_serv)); connect() returns 0 on success 1 on failure
23
Datagram Socket or Connectionless Socket
int dsock; … dsock = socket(AF_INET, SOCK_DGRAM, 0)
24
Datagram receive data int n, remoteL; char buf[256]; struct sockaddr_in remoteS; … n = recvfrom(dsock,buf,256,0, &remoteS, &remoteL);
25
Datagram send data struct sockaddr_in serv; int servL; … n=sendto(dsock, buf, strlen(buf), 0, &serv, servL);
26
Socket in C# Library using System; using System.Net;
using System.Net.Sockets;
27
Create Socket object Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
28
Binding socket & Listen
Stream Socket Binding socket & Listen IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 2048); … newsock.Bind(ipep); newsock.Listen(5);
29
Connect IPEndPoint ipep = new IPEndPoint( IPAddress.Parse(" "), 2048); … server.Connect(ipep);
30
Accept Socket client = newsock.Accept();
31
Send & Receive byte[] data = new byte[1024]; int recv; … string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); client.Send(data, data.Length, SocketFlags.None); recv = client.Receive(data);
32
Close socket client.Shutdown(SocketShutdown.Both); client.Close(); newsock.Shutdown(SocketShutdown.Both); newsock.Close();
33
Datagram Socket Create Socket object: Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
34
Send data SendTo(byte[] data, EndPoint Remote) SendTo(byte[] data, SocketFlags Flags, EndPoint Remote) SendTo(byte[] data, int Size, SocketFlags Flags, EndPoint Remote) SendTo(byte[] data, int Offset, int Size, SocketFlags Flags, EndPoint Remote)
35
Example: byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint( IPAddress.Parse(" "), 9050); … string welcome = "Hello, are you there?"; data = Encoding.ASCII.GetBytes(welcome); server.SendTo(data, data.Length, SocketFlags.None, (Endpoint) ipep);
36
Receive data ReceiveFrom(byte[] data, ref EndPoint Remote)
37
Example: int recv; byte[] data = new byte[1024]; IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); … recv = newsock.ReceiveFrom(data, ref Remote);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.