Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! i have been asked to create a client server program that should be able to swap between TCP and UDP.

I have got this so far, its a basic program that works the area of the circle out...

SERVER code:

Java
package server;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

public class Server extends JFrame {
    //text for displaying contents
    private JTextArea jta = new JTextArea();
    
    public Server(){
        setTitle("Server");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        setLayout(new BorderLayout());
        add(new JScrollPane(jta), BorderLayout.CENTER);
        setVisible(true);
        
        try{
            //create a server socket
            ServerSocket serverSocket = new ServerSocket(8000);
            jta.append("Server started at : "+ new Date() + "\n");
            //listen for connection request
            Socket socket = serverSocket.accept();
            //create data input and output streams
            DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
            DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
            
            while(true){
                //receive radius from client
                double radius = inputFromClient.readDouble();
                //compute area
                double area = Math.PI*radius * radius;
                //send back radius to client
                outputToClient.writeDouble(area);
                jta.append("Radius recieved from client : "+ radius + "\n");
                jta.append("Area found : "+ area + "\n");
            }
        
        }
        catch(IOException ex){
            System.err.println(ex);
        }
        
    }
    
    public static void main(String[] args) {
        new Server();
    }
}


Client code:


Java
package client;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*; 
import java.awt.event.*;

public class Client extends JFrame {
    //text field to receiving radius 
    private JTextField jtf = new JTextField();
    //Text area to display contents
    private JTextArea jta = new JTextArea();
    //input output streams
    private DataInputStream fromServer;// = new DataInputStream(socket.getInputStream());
    private DataOutputStream toServer;// = new DataOutputStream(socket.getOutputStream());
    
    public Client(){
        setTitle("Client");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        setLayout(new BorderLayout());
        setVisible(true);
        
        
        //panel to hold the label and text filed
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.add(new JLabel("Enter radius :"), BorderLayout.WEST);
        p.add(jtf, BorderLayout.CENTER);
        jtf.setHorizontalAlignment(JTextField.RIGHT);
        
        setLayout(new BorderLayout());
        add(p,BorderLayout.NORTH);
        add(new JScrollPane(jta), BorderLayout.CENTER);
        jtf.addActionListener(new TextFieldListener());
        
        
        
        
    
        try{
            //create socket to connect to the server
            Socket socket = new Socket("10.142.132.204", 8000);
            //create an input stream to receive data from the server
            fromServer = new DataInputStream(socket.getInputStream());
            //create an output stream to send data to the server
            toServer = new DataOutputStream(socket.getOutputStream());            
        }
        catch(IOException ex){
            System.err.println(ex);
        }
    }
    private class TextFieldListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
        
            try{
                //get radius from the text field
                double radius = Double.parseDouble(jtf.getText().trim());
                //send radius to server
                toServer.writeDouble(radius);
                toServer.flush();
                //get area from server
                double area = fromServer.readDouble();
                
                //display to th text area
                jta.append("Radius is : "+ radius + "\n");
                jta.append("Area recieved from the server is : "+ area + "\n");
                
            }
            catch(IOException ex){
                System.err.println(ex);
            }
        }
        
    }
    
    
    public static void main(String[] args) {
        new Client();
        
        
    }


now i do not know how to make the client or server choose between TCP or UDP... can i two buttons that have "TCP" and "UDP" on them and when clicked it will use that protocol?

I wouldnt know how to code that though.. could someone be of assistance to me please? I am new new to Java by the way:-)
Posted
Updated 13-Oct-14 0:59am
v2

Have you simply googled it first? Enough info to find, like this:
"http://systembash.com/content/a-simple-java-udp-server-and-udp-client/[^]\

Good luck!
 
Share this answer
 
You just need to use JComboBox to show one option, TCP or UDP, for connection. And two buttons: connect and disconnect. And A popup dialog to show connection status. Then it's easy to switch the two protocols.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900