http://www.java2s.com/Code/Java/Network-Protocol/ServerSocket.htm and here is the client: http://www.java2s.com/Code/Java/Network-Protocol/Socket.htm I think if you familiar with TCP&IP, its simple ........
这是我原来做的一个,不是太好,你将就改改用吧 5public class MultiJabberServer { 6 static final int PORT = 9999; 7 public static void main(String[] args) 8 throws IOException { 9 ServerSocket s = new ServerSocket(PORT); 10 System.out.println("Server Started"); 11 try { 12 while(true) { 13 // Blocks until a connection occurs: 14 Socket socket = s.accept(); 15 try { 16 new IOHandle(socket); 17 } catch(IOException e) { 18 // If it fails, close the socket, 19 // otherwise the thread will close it: 20 socket.close(); 21 } 22 } 23 } finally { 24 s.close(); 25 } 26 } 27 } public class IOHandle extends Thread { 5 private Socket socket; 6 private BufferedReader in; 7 private PrintWriter out; 8 public static int i = 1; 9 10 public IOHandle(Socket s) 11 throws IOException { 12 socket = s; 13 in = 14 new BufferedReader( 15 new InputStreamReader( 16 socket.getInputStream())); 17 // Enable auto-flush: 18 out = 19 new PrintWriter( 20 new BufferedWriter( 21 new OutputStreamWriter( 22 socket.getOutputStream())), true); 23 // If any of the above calls throw an 24 // exception, the caller is responsible for 25 // closing the socket. Otherwise the thread 26 // will close it. 27 start(); // Calls run() 28 } 29 30 public void run() { 31 try { 32 while (true) { 33 String str = in.readLine(); 34 //这里补充下,因为Java这里写的是readLine(),所以传值时别忘了在最后加上\n或者\r\n,要不你会为服务器收不到数据而抓狂的 35 if (str.trim().equals("END")) break; 36 System.out.println("Echoing:" + str.trim()); 37 38 //查询数据库,返回值 39 out.println("ture"); 40 41 } 42 System.out.println("closing"); 43 } catch (IOException e) { 44 System.err.println("IO Exception"); 45 } finally { 46 try { 47 socket.close(); 48 } catch (IOException e) { 49 System.err.println("Socket not closed"); 50 } 51 } 52 } 53}你把发送的东西做成特定格式的字符串用符号隔开也行,直接定义成类,发送的时候发送成字节格式,然后接收时按相应格式接收也行。由于你的状态只有4个,所以比较推荐第一种方式。如果多的话就推荐第二个,然后将状态定义成枚举,要不你自己以后都改不了自己的代码
用socket自己定义一个cmessage类就可以了,根据报文做相应的处理.
6 static final int PORT = 9999;
7 public static void main(String[] args)
8 throws IOException {
9 ServerSocket s = new ServerSocket(PORT);
10 System.out.println("Server Started");
11 try {
12 while(true) {
13 // Blocks until a connection occurs:
14 Socket socket = s.accept();
15 try {
16 new IOHandle(socket);
17 } catch(IOException e) {
18 // If it fails, close the socket,
19 // otherwise the thread will close it:
20 socket.close();
21 }
22 }
23 } finally {
24 s.close();
25 }
26 }
27 }
public class IOHandle extends Thread {
5 private Socket socket;
6 private BufferedReader in;
7 private PrintWriter out;
8 public static int i = 1;
9
10 public IOHandle(Socket s)
11 throws IOException {
12 socket = s;
13 in =
14 new BufferedReader(
15 new InputStreamReader(
16 socket.getInputStream()));
17 // Enable auto-flush:
18 out =
19 new PrintWriter(
20 new BufferedWriter(
21 new OutputStreamWriter(
22 socket.getOutputStream())), true);
23 // If any of the above calls throw an
24 // exception, the caller is responsible for
25 // closing the socket. Otherwise the thread
26 // will close it.
27 start(); // Calls run()
28 }
29
30 public void run() {
31 try {
32 while (true) {
33 String str = in.readLine();
34 //这里补充下,因为Java这里写的是readLine(),所以传值时别忘了在最后加上\n或者\r\n,要不你会为服务器收不到数据而抓狂的
35 if (str.trim().equals("END")) break;
36 System.out.println("Echoing:" + str.trim());
37
38 //查询数据库,返回值
39 out.println("ture");
40
41 }
42 System.out.println("closing");
43 } catch (IOException e) {
44 System.err.println("IO Exception");
45 } finally {
46 try {
47 socket.close();
48 } catch (IOException e) {
49 System.err.println("Socket not closed");
50 }
51 }
52 }
53}你把发送的东西做成特定格式的字符串用符号隔开也行,直接定义成类,发送的时候发送成字节格式,然后接收时按相应格式接收也行。由于你的状态只有4个,所以比较推荐第一种方式。如果多的话就推荐第二个,然后将状态定义成枚举,要不你自己以后都改不了自己的代码
这里说的协议,其实就是你的客户端告诉服务器应该进行什么样的动作
(申请,审核,制作,作废)比如,你需要服务器端对客户端发送的数据进行数据库检索,你就在你的数据前加上一些标志信息,如@search:+客户端数据。服务器收到之后,就对这些标志进行分析,如分析得到:标志为@search:,它就将你的“客户端数据”摘下来,然后检索数据库标志信息由你自己设置。。
把标示信息夹在客户端数据的前面,并且用“;”隔开了,做成Base64编码的字符串发给了服务器,服务器可以正确的接收并且解析数据,服务器拿到标示信息和字符串application比较,通过这种方式实现了申请功能
还需要定义枚举类型吗?怎么定义? 我是以字符串的形式发给服务器的,15 楼的说要定义成类,怎么定义?
String msghead = "application";
String str = msghead + ";" + text.getText() + "," + text_1.getText() + "," + text_2.getText() + "," + text_3.getText() + "," + text_4.getText() + "," + text_5.getText() + "," + text_6.getText() + "," + text_7.getText() + ","+ text_8.getText() + "," + combo_1.getText() + ";" ;
str = new sun.misc.BASE64Encoder().encode(str.getBytes()); OutputStream socketOut = socket.getOutputStream();
socketOut.write(str.getBytes());
这样把数据发给了服务器
服务器端:
socket = serversocket.accept();
InputStream os = socket.getInputStream();
System.out.println("服务器接收到的数据如下:");
byte [] bt= new byte[150];
os.read(bt); //接到的字节读入字节数组bb
String str = new String(bt); //字节数组转换整字符串
byte[] bt2 = new sun.misc.BASE64Decoder().decodeBuffer(str); //Base64解码
String str2 = new String(bt2); // 还原成字符串
System.out.println(str2);
String [] info = str2.split(";");
//由info[0]的值判断操作类型
if(info[0].equals("application"))
{
id=id + 1;
System.out.println("进入申请程序!!");
appAc(info[1],id);
}else if(info[0].equals("check"))
{
System.out.println("进入审核程序!!");
checkAc(info[1]); }else if(info[0].equals("made"))
{
}else if(info[0].equals("cut"))
{
}else if(info[0].equals("search"))
{
}else
{
}
我没有使用线程,再以后的过程是不是不使用线程会比较麻烦?审核的过程要和服务器通信三次,这种该怎么实现?
操作类型和客户端数据组成字符串:String str = types.application + ";" + text.getText() + "," + text_1.getText() + "," + text_2.getText() + "," + text_3.getText() + "," + text_4.getText() + "," + text_5.getText() + "," + text_6.getText() + "," + text_7.getText() + ","+ text_8.getText() + "," + combo_1.getText() + ";" ;然后再服务器端也同样声明一个枚举类型 public enum types{application,check,made,cut};
在解析玩数据后,判断操作类型时:
//由info[0]的值判断操作类型
if(info[0].equals(types.application.toString()))
{
id=id + 1;
System.out.println("进入申请程序!!");
appAc(info[1],id);
}
是不是这样用的,以后自己增加操作类型时就方便多了,条理清晰一些?