IO异常

sky123123 2010-01-09 10:55:56
当我从Socket中读数据时,readUTF()不能读取数据,一直处于阻塞状态,用int i = dis.read(b);时,如果Socket中无数据时,会返回“java.lang.NullPointerException”,这该怎么解决啊,

还有没有从Socket中读取数据时直接返回String的方法啊,

谢谢大家了,O(∩_∩)O~
...全文
190 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky123123 2010-01-15
  • 打赏
  • 举报
回复

while (flag) {
int i = dis.read(b);
temp = new String(b, 0, i);
if (temp.endsWith("0D"))
flag = false;
System.out.println(temp);
}
读取数据时,经常报

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.checkBounds(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at com.love999.testserver.rece.ReceThread.run(ReceThread.java:28)
at java.lang.Thread.run(Unknown Source)


请问这是什么原因造成的?

谢谢!
小驴哥 2010-01-10
  • 打赏
  • 举报
回复
int count = is.read(buffer);
String str = new String(buffer,0,count);
System.out.println("收到了服务器的返回数据:" + str);

这段其中IS 直接用InputStream is = socket.getInputStream()
不用DataInputSream撒
小驴哥 2010-01-10
  • 打赏
  • 举报
回复
哦,看错了,你无视我刚才说的,看看这段代码先
public SocketClient()
{
try {
sc = new Socket("localhost",1234);
//服务器的IP和端口
is = sc.getInputStream();
os = sc.getOutputStream();


} catch (UnknownHostException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}
public void sendToServer(String str)
{
try {
System.out.println("请数据你要发送到服务器的数据:");
os.write(str .getBytes());
System.out.println("向服务器发送了数据:" + str);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public String receive()
{
try {
int count = is.read(buffer);
String str = new String(buffer,0,count);
System.out.println("收到了服务器的返回数据:" + str);
return str;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}


}
public void closeconnection()
{
try {
is.close();
os.close();
sc.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}
zhuyouyong 2010-01-10
  • 打赏
  • 举报
回复
学习下
sagegz 2010-01-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lxyshishen 的回复:]
贴完整撒,你的dis是什么呢?应该是socket才对撒
[/Quote]
不懂别乱说!
dis是DataInputStream的实例,你从哪的API看到Socket有read方法?
小驴哥 2010-01-10
  • 打赏
  • 举报
回复
贴完整撒,你的dis是什么呢?应该是socket才对撒
crazylaa 2010-01-10
  • 打赏
  • 举报
回复
头晕
hongjn 2010-01-10
  • 打赏
  • 举报
回复
智鹿软件 2010-01-09
  • 打赏
  • 举报
回复
“java.lang.NullPointerException”,就是空指针吧!
没找到呀!最好将路径设成全局变量,这样写和读的路径一样,应该不会出问题!
sky123123 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nihaozhangchao 的回复:]
把代码贴出来看看。
[/Quote]


String userInfor = Encapsulate.Encapsu(dataPacket);
byte[] b = new byte[512];
String temp = "";
try {
dis = new DataInputStream(socket.getInputStream());
osw = new OutputStreamWriter(socket.getOutputStream());
osw.write(userInfor);
osw.flush();
System.out.println("发送C1(用户登录)数据包:" + userInfor +" 发送C1(用户登录)数据包:" + userInfor.length());
while(true){
int i = dis.read(b);//这个地方报错
temp = new String(b,0,i);
if(temp.endsWith("0D"))
break;
}
System.out.println("服务器回应:"+temp);
「已注销」 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sky123123 的回复:]
用的是TCP协议啊,先建立连接,然后他给我回应,我在发送用户名和密码登陆,给我回应登陆信息,然后我在传数据给他,问题时他能收到我的数据,我收不到他的回应信息。。
[/Quote]

我这里说的协议,不是指底层的TCP,是说的消息格式。也就是你这里说的用户名,密码,回应信息的格式

比如,这个用户名和密码是怎么发过去的?
writeUTF(用户名);writeUTF(密码);?

那回应信息呢?是一个byte?还是一个字符串?字符串的话是按什么格式呢?
SambaGao 2010-01-09
  • 打赏
  • 举报
回复
把代码贴出来看看。
sky123123 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 redduke1202 的回复:]
不知道?
难道你们通过socket通讯,都不制定传输协议,不打招呼的?各做各的?
[/Quote]


用的是TCP协议啊,先建立连接,然后他给我回应,我在发送用户名和密码登陆,给我回应登陆信息,然后我在传数据给他,问题时他能收到我的数据,我收不到他的回应信息。。
sky123123 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sky123123 的回复:]
引用 2 楼 redduke1202 的回复:
读写要一致,不能牛头对马嘴

另外一头如果是writeUTF写过来的数据,你才能readUTF()读


另外头是用C#发送的,我也不知道他是用什么方式发送的。。。
[/Quote]

用的是TCP协议啊,先建立连接,然后他给我回应,我在发送用户名和密码登陆,给我回应登陆信息,然后我在传数据给他,问题时他能收到我的数据,我收不到他的回应信息。。
「已注销」 2010-01-09
  • 打赏
  • 举报
回复
不知道?
难道你们通过socket通讯,都不制定传输协议,不打招呼的?各做各的?
sky123123 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 redduke1202 的回复:]
读写要一致,不能牛头对马嘴

另外一头如果是writeUTF写过来的数据,你才能readUTF()读
[/Quote]

另外头是用C#发送的,我也不知道他是用什么方式发送的。。。
「已注销」 2010-01-09
  • 打赏
  • 举报
回复
读写要一致,不能牛头对马嘴

另外一头如果是writeUTF写过来的数据,你才能readUTF()读

62,614

社区成员

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

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