关闭

java Tcp Client server

3946人阅读 评论(0) 收藏 举报

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

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:308017次
    • 积分:4962
    • 等级:
    • 排名:第5884名
    • 原创:181篇
    • 转载:0篇
    • 译文:0篇
    • 评论:21条
    文章分类