?? server.java
字號:
package com.test;
import java.io.*;
import java.net.*;
import java.util.*;
public class server
{
public static void main(String args[])
{
ServerSocket ss = null;
Server_thread st;
Socket you = null;
//監聽客戶端,建立與之連接的線程
while(true)
{
try
{
ss = new ServerSocket(4331);
}catch(IOException e){System.out.println("正在監聽"+"Error:"+e);}
try
{
you = ss.accept();
}catch(IOException e){System.out.println("正在等待客戶"+e);}
if(you!=null)
{
new Server_thread(you).start();
}
else
{
continue;
}
}
}
static class Server_thread extends Thread
{
Socket socket;
DataOutputStream out = null;
DataInputStream in = null;
String s = null;
Server_thread(Socket t)
{
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}catch(IOException e){}
}
//處理客戶端傳來的信息,并回應客戶端
public void run()
{
while(true)
{
try
{
s = in.readUTF();
}catch(IOException e){System.out.println("ERROR:"+e);}
try
{
if(s.equals("bye"))
{
out.writeUTF(s);
socket.close();
}
else
{
out.writeUTF("我是服務器,你對我說:"+s);
}
}catch(IOException e){}
}
}
};
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -