




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





(20 Bytes)
(20 Bytes)
TCP Data
IP Datagram
TCP Segment
Source Port Destination Port
Sequence Number
Acknowledgement Number
Header Length
U R G
A C K
P S H
P S T
S Y N
F I N Window Size
Checksum Urgent Pointer
Options (or or more 32 bit words)
Options (or or more 32 bit words)
Options (or or more 32 bit words
package NetProg; import java.net.; import java.io.; public class TCPClient { public static void main(String [] args) { InputStreamReader in = new InputStreamReader(System.in); BufferedReader bufR = new BufferedReader(in); String message = ""; while (true) { try { //create a socket and connect Socket socket = new Socket(โ205.174.40.142โ,4400);
System.out.print("Enter a MESSAGE: "); message = bufR.readLine();
socket.setSendBufferSize(message.length());
//connect to a buffered reader BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), socket.getSendBufferSize()); for (int i=0; i<message.length(); i++){ bos.write((int) message.charAt(i));}
bos.close();
if (message.equals("bye")){ System.out.println("Closing.... goodbye!"); break; }
socket.close(); }//end of try catch (Exception e) { System.out.println("Error " + e.getMessage()); } }//end of while }//end of main }//end of class definition
package NetProg; import java.net.; import java.io.; public class TCPServer { public static void main(String [ ] args) { String message; try { //create a socket and connect ServerSocket server=new ServerSocket(4400); while (true){ Socket socket = server.accept(); //connect to a buffered reader BufferedInputStream binst = new BufferedInputStream(socket.getInputStream(), socket.getReceiveBufferSize()); System.out.print("\n RECEIVED MESSAGE: "); message = ""; for (int i=0;i<socket.getReceiveBufferSize();i++) { int data = binst.read(); if (data == -1) break; else{ System.out.print ((char) data) ; message += (char) (data); } }//end of for message = message.trim(); socket.close(); if (message.equals("bye")){ System.out.println("\n Closing ... goodbye!"); break; }//end of if }//end of while }//end of try catch (Exception e) { System.out.println("Error " + e.getMessage()); }// end of catch }//end of main }//end of class definition