socket的客户端和服务器端的问题 通信的时候,就是互发信息的时候,客户端可以直接和服务器端互发信息么?我见好多都是客户端通过服务器端和另一个客户端互发信息。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 客户端和客户端通过服务器通信以下是客户端import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;public class Client extends Frame { DataOutputStream dos; DataInputStream dis; Socket s = null; TextField tf = new TextField(); TextArea ta = new TextArea(); private boolean ifConnect = false; Thread recvThread = new Thread(new RecvThread()); public static void main(String[] args) { new Client().runFrame(); } public void runFrame() { this.setLocation(400,300); this.setSize(400,300); add(ta,BorderLayout.NORTH); add(tf,BorderLayout.SOUTH); this.pack(); this.addWindowListener(new WindowsMonitor()); tf.addActionListener(new TextFieldLietener()); setVisible(true); connect(); recvThread.start(); } private void connect() { try { s = new Socket("127.0.0.1",8888); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); ifConnect = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private class WindowsMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { try { ifConnect = false; dos.close(); dis.close(); s.close(); } catch(IOException e1) { e1.printStackTrace(); } /*try { ifConnect = false; recvThread.join(); dos.close(); dis.close(); s.close(); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { try { dos.close(); dis.close(); s.close(); } catch (IOException e1) { e1.printStackTrace(); } }*/ System.exit(0); } } private class TextFieldLietener implements ActionListener { public void actionPerformed(ActionEvent arg0) { String str = tf.getText(); tf.setText(""); try { dos.writeUTF(str); dos.flush(); } catch (IOException e) { e.printStackTrace(); } } } private class RecvThread implements Runnable { public void run() { while(ifConnect) { try{ String str = dis.readUTF(); // System.out.println(str); ta.setText(ta.getText() + str + '\n'); } catch(SocketException e) { System.out.println("退出了!"); } catch(IOException e) { e.printStackTrace(); } } } }}以下是服务器import java.io.*;import java.net.*;import java.util.*;public class Server { ServerSocket ss = null; boolean started = false; List<ClientListen> clients = new ArrayList<ClientListen>(); public static void main(String[] args) { new Server().starting(); } public void starting() { try { ss = new ServerSocket(8888); started = true; } catch(BindException e ) { System.out.println("端口使用中..."); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { while(started) { Socket s = ss.accept();//System.out.println("a client connected!"); ClientListen c = new ClientListen(s); new Thread(c).start(); clients.add(c); // dis.close(); } } catch (IOException e) { e.printStackTrace(); }finally { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } class ClientListen implements Runnable { private Socket s; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean ifstart = false; public ClientListen(Socket s ) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); ifstart = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dos.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("对方退出了!"); //e.printStackTrace(); } } public void run() { try{ while(ifstart) { String str = dis.readUTF(); //System.out.println(str); for(int i = 0;i < clients.size();i++) { ClientListen c = clients.get(i); c.send(str); } } } catch(SocketException e) { clients.remove(this); System.out.println("一个客户端已经退出!"); } catch (EOFException e) { System.out.println("客户端退出!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(dis != null) dis.close(); if(dos != null) dos.close(); if(s != null) s.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }} 好像服务器的输入内容必须是socket流提供的吧?那这样就不能通过键盘输入了吧?这样服务器端好像就不能输入了吧? 设计一个类,让它只能实例化3次,怎么实现啊? 一个客户端与服务器传信息的问题,想了2天了没办法,求解! 一个连接数据库的函数,调用关闭函数时出错(附代码) 如何调用字体对话框的类???? 如何做到让java Application只能运行一个实例 java应该用哪些开发程序?? java应用程序画图,请教各位高手,谢谢! 关于同一个包下多个类之间数据实时共享的问题 用JBuilder开发java还要设环境变量吗?急急急 Java 中一个方法的结束,是不是所对应的方法栈就会被回收? 为什么文件复制不了? io流中文件复制的问题
以下是客户端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;public class Client extends Frame { DataOutputStream dos;
DataInputStream dis;
Socket s = null;
TextField tf = new TextField();
TextArea ta = new TextArea();
private boolean ifConnect = false;
Thread recvThread = new Thread(new RecvThread());
public static void main(String[] args) {
new Client().runFrame();
}
public void runFrame() {
this.setLocation(400,300);
this.setSize(400,300);
add(ta,BorderLayout.NORTH);
add(tf,BorderLayout.SOUTH);
this.pack();
this.addWindowListener(new WindowsMonitor());
tf.addActionListener(new TextFieldLietener());
setVisible(true);
connect();
recvThread.start();
}
private void connect() {
try {
s = new Socket("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
ifConnect = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private class WindowsMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) {
try {
ifConnect = false;
dos.close();
dis.close();
s.close();
} catch(IOException e1) {
e1.printStackTrace();
}
/*try {
ifConnect = false;
recvThread.join();
dos.close();
dis.close();
s.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}*/
System.exit(0);
}
} private class TextFieldLietener implements ActionListener { public void actionPerformed(ActionEvent arg0) {
String str = tf.getText();
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} private class RecvThread implements Runnable { public void run() {
while(ifConnect) {
try{
String str = dis.readUTF();
// System.out.println(str);
ta.setText(ta.getText() + str + '\n');
} catch(SocketException e) {
System.out.println("退出了!");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
以下是服务器
import java.io.*;
import java.net.*;
import java.util.*;public class Server {
ServerSocket ss = null;
boolean started = false;
List<ClientListen> clients = new ArrayList<ClientListen>();
public static void main(String[] args) {
new Server().starting();
}
public void starting() {
try {
ss = new ServerSocket(8888);
started = true;
} catch(BindException e ) {
System.out.println("端口使用中...");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept();
//System.out.println("a client connected!");
ClientListen c = new ClientListen(s);
new Thread(c).start();
clients.add(c);
// dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientListen implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean ifstart = false;
public ClientListen(Socket s ) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
ifstart = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!");
//e.printStackTrace();
}
}
public void run() {
try{
while(ifstart) {
String str = dis.readUTF();
//System.out.println(str);
for(int i = 0;i < clients.size();i++) {
ClientListen c = clients.get(i);
c.send(str);
}
}
} catch(SocketException e) {
clients.remove(this);
System.out.println("一个客户端已经退出!");
}
catch (EOFException e) {
System.out.println("客户端退出!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}