创建客户端服务器java程序 [英] Create a client server java program

查看:77
本文介绍了创建客户端服务器java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我被要求创建一个应该能够在TCP和UDP之间交换的客户端服务器程序。



到目前为止我已经有了这个,它是一个基本的程序,适用于圆形区域...



服务器代码:



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:

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:


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();
        
        
    }





现在我不知道如何让客户端或服务器之间做出选择TCP或UDP ...我可以使用两个按钮,它们上面有TCP和UDP,单击它时会使用该协议吗?



我不会知道如何编码虽然..有人可以帮助我吗?顺便说一句,我是Java的新手: - )



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:-)

推荐答案

你是不是先用Google搜索了?有足够的信息可以找到,例如:

http://systembash.com/content/a-simple-java-udp-server-and-udp-client/ [ ^ ] \



好运气!
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!


你只需要使用JComboBox来显示一个选项,TCP或UDP,用于连接。还有两个按钮:connect和disconnect。以及一个显示连接状态的弹出对话框。然后它是很容易切换这两个协议。
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.


这篇关于创建客户端服务器java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆