急,用socket,接收到的数据不对?

hantao2000 2003-12-23 11:30:52
server方代码
public class Server1
{
private ServerSocket ss;
public static final int DAYTIME_PORT = 13;

public static void main(String args[]) throws IOException
{
Server1 server = new Server1();
server.go();
}

public void go() throws IOException
{
Socket s = null;
ss = new ServerSocket(DAYTIME_PORT, 5);
for (;;) {
s = ss.accept();
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
//System.out.println("dddd");
out.write("Current time from server:" +
(new Date()).toString() + "\n");
out.close();
s.close();
}
}
}

客户端代码
public class clientTest {

/** Creates a new instance of clientTest */
public clientTest(String host) {
this.host = host;
}
public static final int DAYTIME_PORT = 13;
String host;
Socket s;

public static void main(String args[]) throws IOException
{
clientTest client = new clientTest("localhost");
client.go();
}


public void go() throws IOException
{
s = new Socket(host, DAYTIME_PORT);
BufferedReader i=new BufferedReader(
new InputStreamReader(s.getInputStream()));

System.out.println(i.read());
i.close();

s.close();
}
}

客户端收到是:67。用socket,接收到的数据不对?为什么?
...全文
49 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hantao2000 2003-12-23
  • 打赏
  • 举报
回复
为什么没有人回答
liuchuan 2003-12-23
  • 打赏
  • 举报
回复
读取数据时用readLine()方法试试。
while ((String l=readLine) != null)
......
lapwing2002 2003-12-23
  • 打赏
  • 举报
回复
System.out.println(i.read());
读入的只是一个字节的数据,所以可能是67,
你可以试试System.out.println(i.readLine());


62,615

社区成员

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

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