Socket programming with UDP and TCP Socket Programming
- Slides: 19
Socket programming with UDP and TCP
Socket Programming with TCP • Connection oriented – Handshaking procedure • Reliable byte-stream
TCP-client in Java import java. io*; import java. net. *; Class TCPClient { public static void main (String argv[]) throws Exception { String sentence; String modified. Sentence; Buffered. Reader in. From. User = new Buffered. Reader( new Input. Stream. Reader(system. in)); Socket client. Socket = new Socket(”hostname”, 6789); Data. Outpu. Stream out. To. Server = new Data. Output. Stream( client. Socket. get. Output. Stream()); Buffered. Reader in. From. Server = new Buffered. Reader(new Input. Stream. Reader( client. Socket. get. Input. Stream())); sentence = in. From. User. read. Line(); out. To. Server. write. Bytes(sentence + ’n’); modified. Sentence = in. From. Server. read. Line(); System. out. println(”FROM SERVER: ” + modified. Sentence); client. Socket. close(); } }
TCP-client in Java import java. io*; import java. net. *; • Imports needed packages Class TCPClient { public static void main (String argv[]) throws Exception { • Standard Java initiation
TCP-client in Java String sentence; String modified. Sentence; • Declares two string objects Buffered. Reader in. From. User = new Buffered. Reader( new Input. Stream. Reader(system. in)) • Creates a stream that handels input from the user
TCP-client in Java Socket client. Socket = new Socket(”hostname”, 6789) ; • Initiate a TCP-connection with the ”hostname” through port 6789 • Client performes a DNS lookup to obtain host IP. Data. Outpu. Stream out. To. Server = new Data. Output. Stream( client. Socket. get. Output. Stream()) • Creates a stream that handels output to server
TCP-client in Java Buffered. Reader in. From. Server = new Buffered. Reader(new Input. Stream. Reader( client. Socket. get. Input. Stream())); • Creates a stream that handels input from server sentence = in. From. User. read. Line() • Puts the input from user into string object
TCP-client in Java out. To. Server. write. Bytes(sentence + ’n’); • Transform sentence to bytes & sends to server modified. Sentence = in. From. Server. read. Line(); • Puts input from server into modified sentence System. out. println(”FROM SERVER: ” + modified. Sentence); client. Socket. close(); } } • Prints modified. Sentence and closes the connection
TCP-server in Java import java. io*; import java. net. *; Class TCPServer { public static void main (String argv[]) throws Exception { String client. Sentence; String capitalized. Sentence; Server. Socket welcome. Socket = new Server. Socket (6789); while (true) { Socket connenction. Socket = welcome. Socket. accept(); Buffered. Reader in. From. Client = new Buffered. Reader( new Input. Stream. Reader( connection. Socket. get. Input. Stream())); Data. Outpu. Stream out. To. Client = new Data. Output. Stream( connection. Socket. get. Output. Stream()); client. Sentence = in. From. Client. read. Line(); capitalized. Sentence = client. Sentence. to. Upper. Case() + ’n’; out. To. Client. write. Bytes(capitalized. Sentence); } }}
TCP-server in Java Server. Socket welcome. Socket = new Server. Socket (6789); • Creates a welcome. Socket that handels connection-attempts from port 6789 Socket connenction. Socket = welcome. Socket. accept(); • Creates a new socket
TCP-server in Java capitalized. Sentence = client. Sentence. to. Upper. Case() + ’n’; • This command is the essence of the application.
Socket Programming with UDP • Connectionless • No reliable datatransfer
UDP-client in Java import java. io. *; import java. net. *; class UDPClient { public static void main(String args[ ]) throws Exception { Buffered. Reader in. From. User = new Buffered. Reader(new Input. Stream. Reader(System. in)); Datagram. Socket client. Socket = new Datagram. Socket(); Inet. Address IPAddress = Inet. Address. get. By. Name(”hostname”); byte[ ] send. Data = new byte[1024]; byte[ ] recieve. Data = new byte[1024]; String sentence = in. From. User. read. Line(); send. Data = sentence. get. Bytes (); Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, 9876); client. Socket. send(send. Packet); Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); client. Socket. receive(receive. Packet); String modefied. Sentence = new String(receive. Packet. get. Data()); System. out. println(”FROM SERVER: ” + modified. Sentence); client. Socket. close(); } }
UDP-client in Java Datagram. Socket client. Socket = new Datagram. Socket(); • This line does not initiate a TCP connection Inet. Address IPAddress = Inet. Address. get. By. Name(”hostname”); • Uses DNS lookup to find the IP-address for ”hostname”
UDP-client in Java byte[ ] send. Data = new byte[1024]; byte[ ] recieve. Data = new byte[1024]; • Arrays that contains bytes that will be sent and recieved Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, 9876); • Creates a packages that containes travel information
UDP-client in Java client. Socket. send(send. Packet); • Sends the package through the client socket Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); • Creates a placeholder for the package client. Socket. receive(receive. Packet); • Rececives the package from the server
UDP-client in Java String modefied. Sentence = new String(receive. Packet. get. Data()); • Extracts the data from the package and puts it in a string package client. Socket. close(); • Closes the client socket
UDP-server in Java import java. io. *; import java. net. *; class UDPClient { public static void main(String args[ ]) throws Exception { Datagram. Socket server. Socket = new Datagram. Socket(9876); byte[ ] receive. Data = new byte[1024]; byte[ ] send. Data = new byte[1024]; while(true) { Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); server. Socket. receive(receive. Packet); String sentence = new String(receive. Packet. get. Data()); Inet. Address IPAddress = receive. Packet. get. Address(); int port = receive. Packet. get. Port(); String catitalized. Sentence = sentence. to. Upper. Case(); send. Data = capetalized. Sentence. get. Bytes (); Datagram. Packet send. Packet = new Datagram. Packet(send. Data, send. Data. length, IPAddress, port); client. Socket. send(send. Packet); Datagram. Packet receive. Packet = new Datagram. Packet(receive. Data, receive. Data. length); server. Socket. send(send. Packet); } }}
UDP-server in Java Datagram. Socket server. Socket = new Datagram. Socket(9876); • Creates a socket at port 9876 that data passes through String sentence = new String(receive. Packet. get. Data()); Inet. Address IPAddress = receive. Packet. get. Address(); int port = receive. Packet. get. Port(); • Extracts the data and client information from the package
- Android udp client
- Udp broadcast c
- Snmp uses tcp or udp
- Livello transport
- Tcp udp
- Fair queuing
- Tcp vs udp
- Nms agent
- Tcp udp
- Tcp dan udp
- Socket.io udp
- C++ udp socket
- Udp socket timeout
- Berkely socket
- Lập trình socket giao tiếp tcp client/server java
- So_rcvlowat
- Tcp echo client server
- 뮤텍스 세마포어
- Client in programming
- Bind function in socket programming