try {
Socket sc = new Socket("127.171.0.101", 10000);

OutputStream os = sc.getOutputStream();
InputStream is = sc.getInputStream();
DataOutputStream out = new DataOutputStream(os);
DataInputStream in = new DataInputStream(is);
  out.write(bb);
  out.flush();
  out.write(cc); 
  out.flush();
  out.write(dd);    
  out.flush();
  System.out.println("发送成功~");
  in.readByte();
  System.out.println("接收成功~");
sc.close();

} catch (Exception e) {
e.printStackTrace();
}
return false;以上是我输入输出的代码块。
我想问客户需要接收到的信息有个判定条件
返回正确 输出 'A' 返回错误 输出 ‘E’ ...请问这个判定条件怎么写!~!~(望指教)。
还是就是红色的地方这样写 能否接收到对方发出的指令。。
指令正确会返回A  错误 返回E。。
希望大虾 帮我改改 写完整!~~1·谢谢!~!~

解决方案 »

  1.   

    自己多看看书!给你举个例子public class ServerSocketTest { public static void main(String[] args) {

    try {
    //服务器端
    ServerSocket ss=new ServerSocket(10000);
    Socket sck=ss.accept();
    //获得输入输出流
    InputStream is=sck.getInputStream();
    OutputStream os=sck.getOutputStream();
    String s=null;
    int size=0;
    byte[] temp=new byte[512];
    while(true&&(size=is.read(temp))!=-1){
    s=new String(temp,0,size);
    System.out.println(s);
    if("exit".equals(s)){
    os.write(temp);
    ss.close();
    }

    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }}//客户端
    public class Client { /**
     * @param args
     */
    public static void main(String[] args) {
    try {
    Socket sc = new Socket("127.171.0.101", 10000); InputStream is = sc.getInputStream();
    OutputStream oos = sc.getOutputStream(); byte[] b = new byte[512];
    Scanner scc = new Scanner(System.in); while (true) {
    String s = scc.nextLine();
    oos.flush();
    oos.write(s.getBytes());
    if ("exit".equals(s)) {
    sc.close();
    break;
    } }

    // byte[] temp=new byte[512];
    // is.read(temp);
    // if(){
    // sc.close();
    // }
    } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } }}
      

  2.   


            DataInputStream in = new DataInputStream(is);
            byte[] buff = new byte[1024];        
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int len = -1;
            while((len = in.read(buff)) != -1){
                buffer.write(buff, 0, len);
            }
            String msg = new String(buffer.toByteArray());
            if(!"".equals(msg) && msg!=null){
                return "A";
            }else{
                return "E";
            }
      

  3.   

    简单写了个,你看符合要求不服务器端:import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;public class ServerTest { public static void main(String[] args) {
    try {
    ServerSocket ss = new ServerSocket(8000);
    Socket socket = ss.accept();
    InputStream ins = socket.getInputStream();
    OutputStream outs = socket.getOutputStream();
    byte[] temp = new byte[512];
    int size = 0;
    while ((size = ins.read(temp)) != -1) {
    String s = new String(temp, 0, size);
    System.out.println("来自客户端的消息:" + s);
    outs.write("返回给客户的信息".getBytes());
    }
    } catch (IOException e) {
    e.printStackTrace();
    } }}客户端import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;public class ClientTest { public static void main(String[] args) {
    try {
    Socket socket =new Socket("127.171.0.101",10000);
    InputStream ins=socket.getInputStream();
    OutputStream outs=socket.getOutputStream();
    byte[] temp=new byte[512];
    String s;
    int size=0;
    outs.write("test".getBytes());
    size=ins.read(temp);
    s=new String(temp,0,size);
    if(s!=null){
    System.out.println("A");
    }else{
    System.out.println("E");
    }
    outs.close();
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }