?? mysocketserver.java
字號:
import java.io.*;
import java.net.* ;
public class MySocketServer
{
public static String data[] = new String[]{ "豬八戒","張三豐","張無忌","孫悟空" };
public static void main(String args[])throws Exception
{
ServerSocket ss = new ServerSocket(8189) ;
Socket s = ss.accept() ;
System.out.println("客戶端已連接") ;
DataInputStream dis = new DataInputStream(s.getInputStream()) ;
DataOutputStream dos = new DataOutputStream(s.getOutputStream()) ;
String line;
try
{
line = dis.readUTF() ;
System.out.println("用戶送出字符串:"+line) ;
process(line.trim(),dos) ;
}catch(Exception e)
{
}
s.close() ;
System.out.println("服務終止") ;
}
public static void process(String cmd,DataOutputStream out)throws Exception
{
boolean find = false ;
for(int i = 0 ; i < data.length ; i++)
{
if(data[i].indexOf(cmd)!=-1)
{
out.writeUTF(data[i]+",") ;
out.flush() ;
find = true ;
}
}
if(!find)
{
out.writeUTF("抱歉,找不到數據") ;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -