import java.net.*;
import java.io.*;
import java.util.*;class Server {
boolean started = false;
ServerSocket ss = null;
List<ClientRun> list = new ArrayList<ClientRun>();
public static void main(String[] args) {
new Server().start();
} public void start()
{
try
{
ss = new ServerSocket(8888);
started = true;
}
catch (BindException be)
{
System.out.println("端口使用中!");
}
catch (IOException e)
{
System.out.println("服务器连接失败!");
}
try
{
while (started) {
Socket s = ss.accept();
ClientRun cr = new ClientRun(s); //主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
new Thread(cr).start(); //把连接进来的Socket传到线程中。
list.add(cr);
System.out.println("系统提示:"+s.getInetAddress()+"已经联入");
}
}
catch (IOException e)
{
System.out.println("Client closed!");
}
}
/***********************************多线程实现多客户端同时连接**************************************************/
class ClientRun implements Runnable
{
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean bConnect = false;
public ClientRun(Socket s)
{
this.s = s;
try
{
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnect = true;
}
catch (IOException e)
{
e.printStackTrace();
}
} public void send(String str)
{
try
{
dos.writeUTF(str);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void run()
{
try
{
while (bConnect)
{
String str = dis.readUTF();
System.out.println(str);
for (int i=0 ;i<list.size() ;i++ )
{
ClientRun cr = list.get(i);
cr.send(str);
}
}
}
catch (Exception e)
{
System.out.println(s.getInetAddress()+"离开了!");
}
finally
{
try
{
if (dis != null) dis.close();
if (dos != null) dos.close();
if (s != null){
s.close();
s = null;
}
}
catch (IOException io)
{
io.printStackTrace();
}
}
}
}
}
解决方案 »
- 我是新手,在学习struts期间,遇到一些关于过滤器的问题
- 解释下如下的问号处
- servlet, struts的安全问题。
- 如何透彻学习spring和hibernate
- oracle中与||功能相同的是哪个?十个选择题,但记不清选项,有谁知道答案呢?
- 为什么老没人回答我问题 (itext处理word)
- 求救:关于hibernate的问题!
- CORBA出错,请高手帮忙,菜鸟问题
- 使用struts开发时,碰到使用window.open()新开窗口时,原窗口的session丢失,500分。
- 【上海】招J2EE程序员(借地方一用)
- 大家看apach的这个网页是干什么的
- 运行web后出现空指针异常,有谁能帮我看看
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;class Client {
public static void main(String[] args) {
MyFrame fram = new MyFrame();
}
}class MyFrame extends Frame {
Panel p1;
Panel p2;
Panel p3;
TextArea show;
TextField input;
Label port;
Label ip;
//Label name;
TextField tPort;
TextField tIp;
//TextField tName;
Button submit;
Button login;
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnect = false; public MyFrame() {
init();
}
public void init() {
/***********************************show begin****************************************************/
p1 = new Panel();
show = new TextArea(15,80);
show.setEditable(false);
p1.add(show);
add(p1,BorderLayout.CENTER);
/***********************************show end****************************************************/ /***********************************Panel2 begin****************************************************/
p2 = new Panel(new FlowLayout(FlowLayout.LEFT));
port = new Label("PORT");
tPort = new TextField(7);
ip = new Label("IP");
tIp = new TextField(25);
//name = new Label("NAME");
//tName = new TextField(7);
login = new Button("login");
p2.add(port);
p2.add(tPort);
p2.add(ip);
p2.add(tIp);
//p2.add(name);
//p2.add(tName);
p2.add(login);
login.addActionListener(new submitAction());
add(p2,BorderLayout.NORTH);
/***********************************Panel2 end****************************************************/
/***********************************Panel3 begin****************************************************/
p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
input = new TextField(70);
submit = new Button("submit");
p3.add(input);
p3.add(submit);
input.addActionListener(new inputAction());
submit.addActionListener(new submitAction());
add(p3,BorderLayout.SOUTH);
/***********************************Panel3 end****************************************************/
/********************************fram begin************************************************/
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
//disConnect();
System.exit(0);
}
});
this.setSize(300, 300);
setLocation(100,100);
pack();
setVisible(true);
/********************************fram end************************************************/
}
class submitAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ("submit".equals(e.getActionCommand())) {
String str = input.getText().trim();
//show.setText(show.getText() + str + '\n');
input.setText("");
faSong(str);
}else if ("login".equals(e.getActionCommand())) {
connect();
}
}
}
/****************************************回车输出事件*****************************************************/
class inputAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = input.getText().trim();
//show.setText(show.getText() + str + '\n');
input.setText("");
faSong(str);
}
}
/*******************************向服务端发送信息*******************************************************/
public void faSong(String str)
{
try
{
dos.writeUTF(str);
dos.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*******************************关闭资源*******************************************************/
/*public void disConnect()
{
try
{
dos.close();
}
catch (IOException e)
{
//e.printStackTrace();
System.out.println("系统错误");
}
}*/
/*******************************************接收服务器发送的信息******************************************************/
private class RecvClient implements Runnable
{
public void run()
{
try
{
while (bConnect)
{
String str = dis.readUTF();
show.setText(show.getText()+str+'\n');
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*******************************connection begin*******************************************************/
public void connect() {
try
{
s = new Socket(tIp.getText(),Integer.parseInt(tPort.getText()));
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
bConnect = true;
new Thread(new RecvClient()).start();
System.out.println("我连进来啦!~哈哈~~");
}
catch (UnknownHostException uhe)
{
uhe.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}