关于(输入输出)接收到的信息问题??

geniusfzq1984 2010-04-20 03:30:22
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·谢谢!~!~
...全文
81 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
duyu1025 2010-04-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 duyu1025 的回复:]
简单写了个,你看符合要求不

服务器端:

Java code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Se……
[/Quote]

端口和IP你自己改下,服务器端口和客户端口是一样的
duyu1025 2010-04-20
  • 打赏
  • 举报
回复
简单写了个,你看符合要求不

服务器端:

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();
}
}
}

yukang_ky 2010-04-20
  • 打赏
  • 举报
回复

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";
}
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
哪位大虾帮一下 小弟 啦 急
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
就是说只要有信息返回就是 A 没有信息返回就是报错的 就是返回一个 E
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
就是判定得到的是否是一段密钥 。。是的 就返回一个 A 错误的就返回一个 E
实在不会写。请帮忙!~!~谢谢!~!~
duyu1025 2010-04-20
  • 打赏
  • 举报
回复
怎么判断正确错误?什么规则?楼主 有点燥~~~
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
帮一下啦 各位大虾!~~
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
请大虾 帮一下 谢谢啦 马山给钱!!~
geniusfzq1984 2010-04-20
  • 打赏
  • 举报
回复
谁帮我写写吧
就是要客户返回的信息时有个判定条件
正确为 A 错误为 E
还是就是我如何写(输出这个方法)可以接收到对方给我的消息(补充 客户是输出 我是输入出的)。。。
求求各位啦!~!~
duyu1025 2010-04-20
  • 打赏
  • 举报
回复
自己多看看书!给你举个例子



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();
}

}

}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧