java Tcp Client server

来源:互联网 发布:php ftp上传 编辑:程序博客网 时间:2024/05/01 08:40

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,
 };

}

原创粉丝点击