发布网友 发布时间:2022-04-23 21:29
共2个回答
热心网友 时间:2023-10-10 05:52
这个是客户端
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("localhost");
port =new TextField("81",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)
{
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;
TextArea name;
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()+"\n");
}catch(Exception e)
{
System.out.println(e.getMessage()+" ss");
}
}
public void send(String txt){
try{
out1.println(txt);
out1.flush();
ta.append(in.readLine()+"\n");
}catch(IOException e)
{
System.out.println(e.getMessage()+"send");
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bye){
send("BYE");
System.exit(0);
}
if (e.getSource()==send)
send(tf.getText());
}
}
这个是服务器
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class server {
private static Map<String,Socket> clientMap=new HashMap<String,Socket>();
public static void main(String[] args) {
int i = 1;
try {
ServerSocket s = new ServerSocket(81);
for (;;) {
Socket incoming = s.accept();
System.out.println("连接成功" + i);
ThreadedEchoHandler teh=new ThreadedEchoHandler(incoming, i);
teh.start();
String name=teh.getClientname();
clientMap.put(name,incoming);
i++;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class ThreadedEchoHandler extends Thread {
Frame f;
TextArea ta;
TextField tf;
Button send, bye;
InputStream ins;
Socket s;
PrintStream out1;
BufferedReader in;
PrintWriter out;
public ThreadedEchoHandler(Socket i, int c) {
incoming = i;
counter = c;
f=new Frame("server");
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(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send(tf.getText());
}
});
bye.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send("bye");
}
});
f.add(ta);
f.add(tf);
f.add(send);
f.add(bye);
f.setSize(300,300);
f.setVisible(true);
}
public String getClientname() {
try {
in = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
out = new PrintWriter(incoming.getOutputStream(), true);
return in.readLine();
} catch (IOException e) {
System.out.println(e.getMessage()+"get");
}
return null;
}
public void send(String context){
out.println(context);
out.flush();
}
public void run() {
try {
boolean done = false;
while (!done) {
String str = in.readLine();
if (str == null)
done = true;
else {
out.println("Echo(" + counter + "):" + str);
ta.append("Echo(" + counter + "):" + str+"\n");
if (str.trim().equals("BYE"))
done = true;
}
}
incoming.close();
} catch (Exception e) {
System.out.println(e.getMessage()+"run");
}
}
private Socket incoming;
private int counter;
}
这个鸟东西是个新手写的。唉,太烂了,我无力吐槽。追问这个不行啊,太菜了。有没有好点的?
追答有模版你就知道怎么搞了嘛。写这个东西毫无技术含量。就是一个socket通信+awt界面,稍微瞄一瞄你就会了,相信自己。
热心网友 时间:2023-10-10 05:52
这个网上多的很。