Stream Socket:

  • A dedicated point-to-point channel between a client and server.
  • Use TCP (Transmission Control Protocol) for data transmission.
  • Lossless and reliable.
  • Sent and received in the same order.

Datagram Socket:

  • No dedicated point-to-point channel between a client and server.
  • Use UDP (User Datagram Protocol) for data transmission.
  • May lose data and not 100% reliable.
  • Data may not received in the same order as sent.

Client Server Example

We will write a program that collects student information from a client and send them to a server. We will passing students information in an object.

Student Class

public class Student implements
 { private String name;
private String street;
private String city;
private String state;
private String zip;
public Student(String name, String street, String city, String state, String zip)
 { = name; 	this.street = street; = city; 		this.state = state; = zip; }
public String getName() { return name; }
public String getStreet() { return street; }
public String getCity() { return city; }
public String getState() { return state; }
public String getZip() { return zip; }


public class StudentServer {
private ObjectOutputStream outputToFile;
private ObjectInputStream inputFromClient;
public static void main(String[] args) {
 new StudentServer(); }
public StudentServer() {
try { // Create a server socket
ServerSocket serverSocket = new ServerSocket(8000); System.out.println("Server started ");
// Create an object ouput stream
outputToFile = new ObjectOutputStream( new FileOutputStream("student.dat", true));
while (true) {
// Listen for a new connection request
Socket socket = serverSocket.accept();
inputFromClient = new ObjectInputStream(socket.getInputStream());
object = inputFromClient.readObject();
// Write to the file
System.out.println("A new student object is stored"); }
catch(ClassNotFoundException ex) {
ex.printStackTrace(); }
catch(IOException ex) {
ex.printStackTrace(); }
finally {
try { inputFromClient.close();
outputToFile.close(); }
catch (Exception ex) {
ex.printStackTrace(); }
} } }


public class StudentClient extends JApplet {
private JTextField jtfName = new JTextField(32);
private JTextField jtfStreet = new JTextField(32);
private JTextField jtfCity = new JTextField(20);
private JTextField jtfState = new JTextField(2);
private JTextField jtfZip = new JTextField(5);
// Button for sending a student to the server
private JButton jbtRegister = new JButton("Register to the Server");
String host = "localhost";
public void init() {
// Panel p1 for holding labels Name, Street, and City
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 1));
p1.add(new JLabel("Name"));
p1.add(new JLabel("Street"));
p1.add(new JLabel("City"));
// Panel jpState for holding state
JPanel jpState = new JPanel();
jpState.setLayout(new BorderLayout());
jpState.add(new JLabel("State"), BorderLayout.WEST);
jpState.add(jtfState, BorderLayout.CENTER);
// Panel jpZip for holding zip
 JPanel jpZip = new JPanel();
jpZip.setLayout(new BorderLayout());
jpZip.add(new JLabel("Zip"), BorderLayout.WEST);
jpZip.add(jtfZip, BorderLayout.CENTER);
// Panel p2 for holding jpState and jpZip
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(jpState, BorderLayout.WEST);
p2.add(jpZip, BorderLayout.CENTER);
//….. Same  for other widgets
// Place p1 and p4 into StudentPanel
JPanel studentPanel = new JPanel(new BorderLayout());
studentPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
studentPanel.add(p1, BorderLayout.WEST);
studentPanel.add(p4, BorderLayout.CENTER);
// Add the student panel and button to the applet
add(studentPanel, BorderLayout.CENTER);
add(jbtRegister, BorderLayout.SOUTH);
jbtRegister.addActionListener(new ButtonListener());
private class ButtonListener implements ActionListener {
 public void actionPerformed(ActionEvent e) {
try {
// Establish connection with the server
Socket socket = new Socket(host, 8000);
ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
// Get text field
String name = jtfName.getText().trim();
String street = jtfStreet.getText().trim();
String city = jtfCity.getText().trim();
String state = jtfState.getText().trim();
String zip = jtfZip.getText().trim();
// Create a Student object and send to the server
Student s = new Student (name, street, city, state, zip);
catch (IOException ex) { System.err.println(ex); } } }
public static void main(String[] args) {
 // Create a frame
 JFrame frame = new JFrame("Register Student Client");
// Create an instance of the applet
StudentClient applet = new StudentClient();
// Add the applet instance to the frame
frame.add(applet, BorderLayout.CENTER);
// Display the frame
frame.setVisible(true); } }
Tagged with: JAVAProgramming

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.