请教一个socket 程序问题? 想实现这样的效果,client 不断的向server 发送消息,server 接收到消息之后原路返回般的给client 写些信息。请问该如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.net.*; import java.io.*; import java.lang.*; public class myserver{ public static void main(String args[]){ ServerSocket server; Socket socket; String s; InputStream Is; OutputStream Os; DataInputStream DIS; PrintStream PS; try{ //在端口4321注册服务 server=new ServerSocket(4321); socket=server.accept(); //监听窗口,等待连接 System.out.println("server ok"); System.out.println("************************************************"); System.out.println(""); //获得对应Socket的输入/输出流 Is=socket.getInputStream(); Os=socket.getOutputStream(); //建立数据流 DIS=new DataInputStream(Is); PS=new PrintStream(Os); DataInputStream in=new DataInputStream(System.in); while(true){ System.out.println(""); System.out.println("please wait client's message..."); System.out.println(""); s=DIS.readLine(); //读入从client传来的字符串 System.out.println("client said:"+s); //打印字符串 if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 System.out.print("you say:"); s=in.readLine(); //读取用户输入的字符串 PS.println(s); //将读取得字符串传给client if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接 DIS.close(); //关闭数据输入流 PS.close(); //关闭数据输出流 Is.close(); //关闭输入流 Os.close(); //关闭输出流 socket.close(); //关闭sockey } catch(Exception e){ System.out.println("Error:"+e); } } } 通话器客户端 import java.net.*; import java.io.*; import java.lang.*; public class myclient{ public static void main(String args[]){ if (args.length<1){ //判断命令加参数没有 System.out.println("you forget the name of the server!"); System.out.println("see also: myclient yxf"); System.exit(1); //如果没加参数就退出 } Socket socket; String s="[email protected]"; String len; InputStream Is; OutputStream Os; DataInputStream DIS; PrintStream PS; try{ //向主机名为args[0]的服务器申请连接 //注意端口号要与服务器保持一致:4321 socket=new Socket(args[0],4321); System.out.println("client ok"); System.out.println("************************************************"); System.out.println(""); //获得对应socket的输入/输出流 Is=socket.getInputStream(); Os=socket.getOutputStream(); //建立数据流 DIS=new DataInputStream(Is); PS=new PrintStream(Os); DataInputStream in=new DataInputStream(System.in); while(true){ System.out.print("you say:"); s=in.readLine(); //读取用户输入的字符串 PS.println(s); //将读取得字符串传给server if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 else { System.out.println(""); System.out.println("please wait server's message..."); System.out.println(""); } s=DIS.readLine(); //从服务器获得字符串 System.out.println("server said:"+s); //打印字符串 if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接 DIS.close(); //关闭数据输入流 PS.close(); //关闭数据输出流 Is.close(); //关闭输入流 Os.close(); //关闭输出流 socket.close(); //关闭socket } catch(Exception e){ System.out.println("Error:"+e); } } } http://fanqiang.chinaunix.net/a4/b5/20011230/08300025.html原文 这个程序的客户端和服务器端都是手动输入字符串的,请问如何自动收发字符串,用write() 方法? Collections中的shuffle方法 字符串转换为日期格式 排序问题 图片如何用CM作为计量单位 数据库问题 java高难度图形!!! java.lang.NoSuchMethodError: main Applet放在html页面中就不好用了??? 日期对象GregorianCalendar的bug ? 请问Java如何做应用服务器????? 请教@override注释 JSP动态表单中 传递多个Checkbox参数值的问题!!!!
import java.io.*;
import java.lang.*; public class myserver{
public static void main(String args[]){
ServerSocket server;
Socket socket;
String s;
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS; try{
//在端口4321注册服务
server=new ServerSocket(4321);
socket=server.accept(); //监听窗口,等待连接 System.out.println("server ok");
System.out.println("************************************************");
System.out.println(""); //获得对应Socket的输入/输出流
Is=socket.getInputStream();
Os=socket.getOutputStream();
//建立数据流
DIS=new DataInputStream(Is);
PS=new PrintStream(Os);
DataInputStream in=new DataInputStream(System.in);
while(true){
System.out.println("");
System.out.println("please wait client's message...");
System.out.println("");
s=DIS.readLine(); //读入从client传来的字符串
System.out.println("client said:"+s); //打印字符串
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出
System.out.print("you say:");
s=in.readLine(); //读取用户输入的字符串
PS.println(s); //将读取得字符串传给client
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接
DIS.close(); //关闭数据输入流
PS.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭sockey
}
catch(Exception e){
System.out.println("Error:"+e);
}
}
}
通话器客户端
import java.net.*;
import java.io.*;
import java.lang.*; public class myclient{
public static void main(String args[]){
if (args.length<1){ //判断命令加参数没有
System.out.println("you forget the name of the server!");
System.out.println("see also: myclient yxf");
System.exit(1); //如果没加参数就退出
} Socket socket;
String s="[email protected]";
String len;
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS;
try{
//向主机名为args[0]的服务器申请连接
//注意端口号要与服务器保持一致:4321
socket=new Socket(args[0],4321); System.out.println("client ok");
System.out.println("************************************************");
System.out.println(""); //获得对应socket的输入/输出流
Is=socket.getInputStream();
Os=socket.getOutputStream();
//建立数据流
DIS=new DataInputStream(Is);
PS=new PrintStream(Os);
DataInputStream in=new DataInputStream(System.in); while(true){
System.out.print("you say:");
s=in.readLine(); //读取用户输入的字符串
PS.println(s); //将读取得字符串传给server
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出
else
{
System.out.println("");
System.out.println("please wait server's message...");
System.out.println("");
}
s=DIS.readLine(); //从服务器获得字符串
System.out.println("server said:"+s); //打印字符串
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 } //关闭连接
DIS.close(); //关闭数据输入流
PS.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭socket
}
catch(Exception e){
System.out.println("Error:"+e);
}
}
}