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

📄 client.java

📁 简单的客户端
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Client extends Frame implements ActionListener
{
	int i = 1;
	Frame f;
	TextField ip, port;
	Label Lip, Lport;
	Button connect, exit;
	public static void main(String args[])
	{
		Client c1 = new Client();
		c1.init();
	}
	
	public void init()
	{
		f=new Frame("client connection");
		f.setLayout(new FlowLayout());
		
		ip=new TextField("127.0.0.1", 20);
		port = new TextField("8189",5);
		Lip = new Label("ip address");
		Lport = new Label("port");
		connect = new Button("connect");
		exit = new Button("exit");
		connect.addActionListener(this);
		exit.addActionListener(this);
		f.add(Lip);
		f.add(ip);
		f.add(Lport);
		f.add(port);
		f.add(connect);
		f.add(exit);
		f.setSize(500,60);
		f.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == exit)
		System.exit(0);
		if(e.getSource() == connect)
		{
			f.disable();
			new Thread(new Threadclient(ip.getText(),port.getText(),i)).start();
			i++;
		}
	}
}

 class Threadclient extends Frame implements Runnable,ActionListener
{
	String ip,port;
	int no;
	Frame f;
	TextArea ta;
	TextField tf;
	Button send,bye;
	InputStream ins;
	Socket s;
	PrintStream out1;
	BufferedReader in;
	BufferedWriter out;
	Threadclient(String n, String m,int i)
	{
		ip = n;
		port = m;
		no = i;
	}
	
	public void run()
	{
		f = new Frame("client NO."+no);
		f.setLayout(new FlowLayout());
		ta = new TextArea("",10,30,TextArea.SCROLLBARS_BOTH);
		tf = new TextField("",20);
		send = new Button("send");
		bye = new Button("bye");
		send.addActionListener(this);
		bye.addActionListener(this);
		f.add(ta);
		f.add(tf);
		f.add(send);
		f.add(bye);
		f.setSize(300,300);
		f.setVisible(true);
		Integer tmp = new Integer(port);
		int portint = tmp.intValue();
		try
		{
			s = new Socket(ip,portint);
			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			out1 = new PrintStream(s.getOutputStream());
			ta.append(in.readLine());
			out1.println("hehe haha");
			ta.append(in.readLine() + "\n");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void send(String txt)
	{
		try
		{
			out1.println(txt);
			out1.flush();
			ta.append(in.readLine() + "\n");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == bye)
		{
			send("BYE");
			System.exit(0);
		}
		if(e.getSource() == send)
		send(tf.getText());
	}
}

⌨️ 快捷键说明

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