⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.java

📁 一个简单的网络通信工具 由一个服务器端和一个客户端组成 简单 但是较为完善
💻 JAVA
字号:
package edu.ustb.kang40550590.db;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.*;




public class Connect extends JFrame
 {
     /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    public Connect(String title)
    {
        super(title);
        initialize();
    }
    private JTextField textField;
    private JTextArea textArea;
    private void initialize()
	    {   textField=new JTextField(25);
	        textField.setEnabled(false);
	        textField.addActionListener(new ActionListener()
	        {
	        	public void actionPerformed(ActionEvent event)
	        	{
	        		sendMessage(event.getActionCommand());
	        		
	        	}
	        });
	        textArea=new JTextArea();
	        Container container=getContentPane();
	        JPanel p=new JPanel();
	        p.add(new JLabel("信息:"));
	        p.add(textField);
	        container.add(p,BorderLayout.SOUTH);
	        container.add(new JScrollPane(textArea),BorderLayout.CENTER);
	      	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        setSize(350,200);
	        setVisible(true);
	    } 

	public void run() throws IOException
    {
		textArea.setText("尝试连接服务器...");
		try
		{   clientConnection= new Socket(InetAddress.getByName(null),9000);
			
		    while(true)
	    	{   textArea.append("连接"+clientConnection.getInetAddress().getHostName());
	            outPutStream=new ObjectOutputStream(clientConnection.getOutputStream());
			    outPutStream.flush();
			    inPutStream=new ObjectInputStream(clientConnection.getInputStream());
			    lj();
			}
		}
	    	catch (EOFException eofException)
		    {
		    	System.out.println("服务器端终止连接");
		    }
    }
	
	private ObjectOutputStream outPutStream;
	private ObjectInputStream inPutStream;
	private Socket clientConnection;

	
	
	private void lj() throws IOException
    {
	    String message="建立连接成功!";
	    outPutStream.writeObject(message);
	    outPutStream.flush();
	    textField.setEnabled(true);
	    
	    do
	    {
	    	try
	    	{
	    	    message=(String) inPutStream.readObject();
	    		textArea.append("\n服务器消息  "+message);
	    		textArea.setCaretPosition(textArea.getText().length());
	        }
	    	catch(ClassNotFoundException classNotfoundException)
	    	{
	    		textArea.append("\n接收消息出错");
	 	    }
	    }
	    while(true);
	}


    private void sendMessage(String message)
    {
	  Calendar time=Calendar.getInstance();
	  try
	    {   
		    outPutStream.writeObject(new String(time.getTime().toString().getBytes(),10,10)+":\n"+message);
	    	outPutStream.flush();
	    	textArea.append("\n客户端消息  "+new String(time.getTime().toString().getBytes(),10,10)+":\n"+message);
	    	textField.setText("");
	    }
	    catch(IOException ioException)
	    {
	    	textArea.append("\n发送消息时出错");
	    }
	}
    public static void main(String args[]) throws IOException
    {
      Connect connect=new Connect("客户端");
        connect.run();
    }
	
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -