java Tcp Client server

ServicerRead


import java.net.*;
import java.io.*;

class ServicerRead implements Runnable
{
 public  static byte MSG_START[] = {0x0b,0x02};
 public  static byte MSG_END[] = {0x03,0x1c};
 private Socket s;
 public ServicerRead(Socket s)
 {
  this.s=s;
 }
 public void run()
 {
  try
  {
   InputStream ips=s.getInputStream();
   BufferedInputStream bis;
   int ter=0;
   bis = new BufferedInputStream(ips);
   int ch;
   while(true)
   {
    while ((ch=bis.read()) !=11)
    {
     System.out.print(ch+"----");
     if (ch==-1)
     {
      Thread.sleep(1000);
     }
    }
    System.out.println(ch+"outwhile000000000000000000000");
    if ((ch=bis.read())== 2)
    {
     while (true)
     {
      if ((ch=bis.read()) !=3)
      {
       if (ch==-1)
       {
        System.out.println("END of EOF----");
        //System.exit(0);
        Thread.sleep(1000);
       }
       System.out.print(ch+"/t");
      }
      else
      {
       if ((ch=bis.read()) !=28)
       {
        System.out.print(3+"/t"+ch+"/t");
        //System.out.println("not second end char");
       }
       else
       {
        System.out.println("END of correct"+ter);
        break;
       }
      }
     }
    }
    System.out.println("NEXT");
   } 
   //ips.close();
   //ops.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}
public class Tcpread {
 public static void main(String[] args)
 {
  System.out.println("begin");
  try
  {
   ServerSocket ss=new ServerSocket(8003);
   boolean bRunning=true;
   while(bRunning)
   {
    Socket s=ss.accept();
    new Thread(new ServicerRead(s)).start();
   }   
   ss.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

 

Tcpsend 


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Tcpsend {
 public static  BufferedOutputStream bos;//created by xf 2004/06/09,
 public static  BufferedInputStream bis;
 public static void main(String[] args) {
  System.out.println(" sender is begin");
  
  try
  {
   Socket ss=new Socket("172.16.10.2",8003);
        bos=new BufferedOutputStream(ss.getOutputStream());
        bis=new BufferedInputStream(ss.getInputStream());
   boolean bRunning=true;
   //while(bRunning)
   //{
       if(DealwithBuffer.sbuffer==null)return;
       try{
         bos.write(DealwithBuffer.sbuffer);
         bos.flush();
         System.out.println("fa song end");
       }
       catch(Exception e){
         System.out.println("Sending Answer:" + e.toString());
         //LogServer.debug("Sending Answer"+ e.toString());
       }
   //}   
   ss.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }

}

 

 

 


public class DealwithBuffer {
 public  static byte MSG_START[] = {0x0b,0x02};
 public  static byte MSG_END[] = {0x03,0x1c};
 public  static final byte [] sbuffer={
   3,3,3,33,26,
   0x0b,0x02,
   0,2,5,5,3,1,2,5,
   0x03,0x1c,
   3,4,
   0x0b,0x02,
   1,3,5,5,5,2,1,2,
   0x03,0x1c,
   5,
   0x0b,0x02,
   2,2,5,5,5,2,1,2,
   0x03,0x1c,
   5,
   0x0b,0x02,
   3,2,5,5,5,-1
   //0xc,
 };

}

Java中实现TCP ServerTCP Client可以使用Java提供的Socket类和ServerSocket类。 下面是一个简单的TCP Server实现: ```java import java.net.*; import java.io.*; public class TCPServer { public static void main (String [] args ) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(9999); } catch (IOException e) { System.err.println("Could not listen on port: 9999."); System.exit(1); } Socket clientSocket = null; System.out.println ("Waiting for connection....."); try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } System.out.println ("Connection successful"); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println ("Server: " + inputLine); out.println(inputLine); if (inputLine.equals("Bye.")) break; } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ``` 上述代码创建了一个ServerSocket并监听9999端口。当客户端连接后,程序会创建一个Socket并开始读取客户端发送的数据。服务器将客户端发送的数据原样返回,并在客户端发送“Bye.”后关闭连接。 下面是一个简单的TCP Client实现: ```java import java.net.*; import java.io.*; public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { socket = new Socket("localhost", 9999); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: localhost."); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("Server: " + in.readLine()); if (userInput.equals("Bye.")) break; } out.close(); in.close(); stdIn.close(); socket.close(); } } ``` 上述代码创建了一个Socket并连接到服务器的9999端口。客户端从标准输入读取数据,并将其发送到服务器。客户端接收服务器发送的数据,并将其打印到控制台。 这只是一个简单的示例,实际应用中需要考虑更多的异常情况和数据处理方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值