java 怎么通过 Socket服务端 传参数

MattHgh 2008-01-23 06:21:11
服务端代码如下


import java.io.*;
import java.net.*;

public class EchoServer
{
public static void main(String[] args)
{
try{
ServerSocket s = new ServerSocket(5050);
Socket incoming = s.accept();

BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */);

out.println("55 7F 00 00 01 30");

boolean done = false;
while (!done)
{
String line = in.readLine();
if (line == null)
done = true;
else{
out.println("You to Server : " + line);
System.out.println("Client to you :"+line);
if (line.trim().equals("BYE"))
done = true;
}

}

incoming.close();
}
catch (Exception e){
System.out.println(e);
}
}

}


等客户端连接上后
out.println("55 7F 00 00 01 30");是往客户端发送数据吗
如果不是应该怎么给客户端发送数据
...全文
217 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gary_jojo 2008-01-24
  • 打赏
  • 举报
回复
this.socket.getOutputStream().write(message);
this.socket.getOutputStream().flush();
duskwaitor 2008-01-23
  • 打赏
  • 举报
回复
黑体字和楼主利用PrintWriter发送信息是等效的。这个黑体字填上程序很二,呵呵
z3h 2008-01-23
  • 打赏
  • 举报
回复
是王客户端发的。
duskwaitor 2008-01-23
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
try{
ServerSocket s = new ServerSocket(5050);
Socket incoming = s.accept();

BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
ObjectOutputStream writer = new ObjectOutputStream(incoming.getOutputStream());
PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */);

out.println("55 7F 00 00 01 30");

boolean done = false;
while (!done)
{
String line = in.readLine();
if (line == null)
done = true;
else{
out.println("You to Server : " + line);
System.out.println("Client to you :"+line);

if (line.trim().equals("BYE"))
done = true;
}

}
writer.writeObject("this is the message given to client");
writer.flush();

incoming.close();
}
catch (Exception e){
System.out.println(e);
}
}
老紫竹 2008-01-23
  • 打赏
  • 举报
回复
干吗发2次哦?

62,623

社区成员

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

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