📄 serverframe.java
字号:
//小组成员:秦星
//本程序为服务器,它也具有发送信息的功能
//本程序使用了一个循环监听多客户,当监听到后创建服务程序
//使用了c-s模型
package pack_chat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class serverFrame extends Thread implements ActionListener
{
static Frame f;
TextArea ta;
TextField tf;
Button b3;
Label l;
Choice cs;
Panel p,p1;
ServerSocket s=null,s2=null,ss=null;
Socket sl,sll;
OutputStream slout;
DataOutputStream dos;
InputStream inn;
DataInputStream dnn;
Color mycolor,mycolor1;
int mark=0;
public void init()//构造函数
{
f=new Frame("host聊天室");
f.addWindowListener(new exiting());
ta=new TextArea("recieve massage ",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
ta.setEditable(false);
mycolor=new Color(255,251,210);
ta.setBackground(mycolor);
ta.setForeground(Color.red);
tf=new TextField();
p1=new Panel();
f.add(ta,"Center");
f.add(p1,"South");
p1.setLayout(new BorderLayout());
p1.add(tf,"Center");
mycolor=new Color(233,189,238);
l=new Label("选择发送对象:");
l.setBackground(mycolor);
cs=new Choice();
cs.addItem("Client1(5555)");
//cs.addItem("Client2(4444)");
b3=new Button("发送");
b3.addActionListener(this);
p1.add(b3,"East");
p=new Panel();
p.setLayout(new GridLayout(8,1));
p.add(new Panel());
p.add(l);
p.add(new Panel());
p.add(cs);
f.add(p,"West");
f.setSize(500,300);
f.setVisible(true);
try{s=new ServerSocket(443);} //接收时使用
catch(IOException e){}
String str;int port;//发送时使用
str=cs.getSelectedItem();
port=Integer.parseInt(str.substring(8,12));
try{ss=new ServerSocket(port);}
catch(IOException e){}
this.start();
Thread st=new send();//创建线程
st.start();
}
//.....................................................
public static void show()
{
serverFrame that=new serverFrame();
if(serverFrame.f!=null)serverFrame.f.setVisible(true);
else that.init();
}
class send extends Thread//内部类线程1
{
public void run()//判断客户是否连接上
{
try{
while(true)
{
sl=ss.accept();
mark=1;
}
}
catch(IOException e){}
}
}
public void run()
{
int port=443;
try{
while(true)//循环监听支持多客户,建立两个连接
{
sll=s.accept();
//port++;
connection c=new connection(sll);
}
}
catch(IOException e){}
}
public class connection extends Thread//内部类线程2
{
InputStream slIn;
DataInputStream dis;
OutputStream out;
DataOutputStream dout;
Socket s2;
public connection(Socket s)
{
s2=s;
try{
slIn=s2.getInputStream();
dis=new DataInputStream(slIn);
out=s2.getOutputStream();
dout=new DataOutputStream(out);
}
catch(IOException e){}
this.start();
}
public void run()
{
while(true)
{
try{
try{
Thread.sleep((int)(Math.random()*10));}
catch(InterruptedException e){}
String st=new String(dis.readUTF());
ta.append("\n"+"From Client:"+st);
dout.writeUTF(st);
dout.close();
dis.close();
slIn.close();
s2.close();
}
catch(IOException e){}
}
}
}
public void actionPerformed(ActionEvent e)
{
server();//发送
}
public boolean server()
{
if(mark==1)
{
if(tf.getText().equals("")== true)
ta.append("\n"+"can't be sent without message!");
else
{
try{
slout=sl.getOutputStream();
dos=new DataOutputStream(slout);
dos.writeUTF(tf.getText());
inn=sl.getInputStream();
dnn=new DataInputStream(inn);
ta.append("\n"+"From Host:"+tf.getText());
}
catch(IOException e)
{
ta.append("\n"+"Exception"+e);
return false;
}
}
return true;
}
else
{
ta.append("\n"+"unkownClent");
return false;
}
}
class exiting extends WindowAdapter//内部类3
{
public void windowClosing(WindowEvent e)
{
serverFrame.f.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -