[JAVA] Tcp客户端和服务器简单代码

 

[JAVA] Tcp客户端和服务器简单代码

服务器:

 1 import java.io.DataInputStream;

 2 import java.io.DataOutputStream;

 3 import java.io.IOException;

 4 import java.net.ServerSocket;

 5 import java.net.Socket;

 6 

 7 public class TcpServer {

 8     public static void main(String [] args) throws IOException

 9     {

10         @SuppressWarnings("resource")

11         ServerSocket server=new ServerSocket(8000);

12         Socket s=null;

13         DataOutputStream dataOut=null;

14         DataInputStream  dataIn=null;

15         while(true){

16             try{

17                 s=server.accept();

18                 dataOut=new DataOutputStream(s.getOutputStream());

19                 dataIn =new DataInputStream(s.getInputStream());

20                 while(true){

21                     Byte c=dataIn.readByte();

22                     if(c=='0'){

23                         System.out.println("执行继电器快关变换!");

24                         dataOut.writeByte('Y');

25                     }else if(c=='w'){

26                         System.out.println("将温度值传给服务器!");

27                         dataOut.writeByte(2);

28                     }else if(c=='s'){

29                         System.out.println("将深度值传给服务器!");

30                         dataOut.writeByte(4);

31                     }else{

32                         System.out.println("错误命令返回!");

33                         dataOut.writeByte('N');

34                     }

35                 }

36             }catch(IOException e){}

37             dataOut.close();

38             dataIn.close();

39             s.close();

40         }

41     }

42 }

客户端代码:

 1 /*

 2 TCP客户端:输入命令从服务器获得数据

 3 输入:0即开关继电器,当服务器执行好任务时返回89

 4     w获得温度数据,服务器返回温度数据

 5     s获得水深数据,服务器返回水深数据

 6     其他输入均为无效输入,服务器返回78

 7 PS:服务器只接受1个char,返回也是一个char,上述数据均为16进制

 8 */

 9 import java.io.DataInputStream;

10 import java.io.DataOutputStream;

11 import java.io.IOException;

12 import java.net.InetAddress;

13 import java.net.Socket;

14 import java.util.Scanner;

15 

16 public class TcpClient {

17     public static void main(String [] args) throws IOException 

18     {

19         Socket s = null;

20         DataInputStream DataIn = null;

21         DataOutputStream DataOut = null;

22         try {

23             //s=new Socket(InetAddress.getByName("192.168.1.130"),8000);

24             s=new Socket("localhost",8000);

25             DataIn = new DataInputStream(s.getInputStream());

26             DataOut=new DataOutputStream(s.getOutputStream());

27             System.out.println("连接成功");

28             @SuppressWarnings("resource")

29             Scanner keyIn= new Scanner(System.in);

30             while(keyIn.hasNext()){

31                 String c=keyIn.nextLine();

32                 System.out.println("输入: "+c);

33                 if(c.length()==0)continue;

34                 DataOut.writeByte(c.charAt(0));

35                 System.out.println("收到: "+DataIn.readByte());

36             }

37             DataIn.close();

38             DataOut.close();

39             s.close();

40         } catch (IOException e) {

41             // TODO Auto-generated catch block

42             e.printStackTrace();

43         }

44         DataIn.close();

45         DataOut.close();

46         s.close();

47     }

48 }

 

你可能感兴趣的:(java)