?? server.java
字號:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.net.InetAddress;
public class Server extends JFrame
{
JTextArea abc=new JTextArea();
JPanel contentPane;
JLabel jLabel1 = new JLabel();
TextArea showip = new TextArea();
JLabel jLabel2 = new JLabel();
JTextArea jTextArea2 = new JTextArea();
TextArea sendmessage=new TextArea();
JButton jButton1 = new JButton();
ServerSocket serversocket;
Socket[] socketArry=new Socket[10];
public Server()
{
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(250, 300));
setTitle("Frame Title");
sendmessage.setBounds(0,0,100,100);
jLabel1.setText("顯示上線人IP:");
jLabel1.setBounds(new Rectangle(13, 5, 122, 21));
showip.setBounds(new Rectangle(10, 30, 201, 100));
jLabel2.setText("上線人數:");
jLabel2.setBounds(new Rectangle(12, 134, 152, 24));
jTextArea2.setBounds(new Rectangle(9, 160, 199, 34));
jButton1.setBounds(new Rectangle(71, 209, 71, 29));
jButton1.setText("關閉");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//contentPane.add(sendmessage);
contentPane.add(jLabel1);
contentPane.add(showip);
contentPane.add(jLabel2);
contentPane.add(jTextArea2);
contentPane.add(jButton1);
setVisible(true);
try{
serversocket=new ServerSocket(9999);
for(int i=0;i<socketArry.length;i++)
{
socketArry[i]=serversocket.accept();
InetAddress netaddress=socketArry[i].getInetAddress();
showip.append(netaddress.toString());
showip.append(" \n");
new Myserver(socketArry[i]);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
new Server();
}
public void jButton1_actionPerformed(ActionEvent e) {
System.exit(0);
}
class Myserver implements Runnable
{
Thread thread;
Socket socket;
DataInputStream instream;
DataOutputStream outstream;
int cout;
String get,send,str3;
byte[] buf=new byte[2555];
public Myserver(Socket sock)
{
this.socket=sock;
thread=new Thread(this);
thread.start();
}
public void run()
{
try{
instream=new DataInputStream(socket.getInputStream());
outstream=new DataOutputStream(socket.getOutputStream());
//outstream.writeUTF("Welcome "+socket.getLocalAddress());
while(true)
{
get=instream.readUTF();
if(get.substring(get.length()-6).equals(" 歡迎光臨"))
{
abc.append("\n");
abc.append(get);
str3=abc.getText();
outstream.writeUTF(str3);
}
else{
//if(!get.equals(""))
sendmessage.append(get);
sendmessage.append("\n");
get=sendmessage.getText();
outstream.writeUTF(get);
// System.out.println(get);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
}
/*}
public class Server implements Runnable
{
public Thread thread;
public int cout;
public String s;
public byte[] buf=new byte[100];
public ArrayList list=new ArrayList();
public void run()
{
try
{
String str;
ServerSocket serversocket=new ServerSocket(5555);
while(true)
{
Socket socket=serversocket.accept();
list.add(socket);
new RWthread(socket);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
void start()
{
thread=new Thread(this);
thread.start();
}
public static void main(String args[])
{
Server app=new Server();
app.start();
}
}
class RWthread implements Runnable
{
public Socket mysocket=new Socket();
public int cout1;
public byte[] buf1=new byte[100];
public RWthread(Socket sock)
{
this.mysocket=sock;
Thread thread1=new Thread();
thread1.start();
}
public void run()
{
try{
DataInputStream instream=new DataInputStream(mysocket.getInputStream());
DataOutputStream outstream=new DataOutputStream(mysocket.getOutputStream());
String str="Hello";
outstream.writeUTF(str);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -