如何写client/server通讯程序?在线等待 没听懂,C/S程序在一台机器上实现和在两台机器间实现有什么区别? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是要建一个服务器吗? 网上的c/s例子程序都只能在同一台机器上实现??!!不要乱下结论 import java.io.*;import java.net.*;public class EchoServer1{ public static void main(String[] args ){ try{ServerSocket s = new ServerSocket(8500);Socket incoming = s.accept( );BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */);out.println( "Hello! Enter BYE to exit." );boolean done = false;while (!done){ String line = in.readLine();if (line == null) done = true;else{ out.println("Echo: " + line);if (line.trim().equals("BYE"))done = true;}}incoming.close();}catch (Exception e){ System.out.println(e);}}}// EchoClient1.javaimport java.io.*;import java.net.*;public class EchoClient1 { public static void main(String args[]) { try{ if (args.length != 1){ System.out.println("USAGE: java Client servername"); return; } String connectto= args[0]; Socket connection; // connect to server if(connectto.equals("localhost")){ connection=new Socket(InetAddress.getLocalHost(),8500); } else{ connection=new Socket(InetAddress.getByName(connectto),8500); } BufferedReader input=new BufferedReader(new InputStreamReader(connection.getInputStream())); PrintWriter out = new PrintWriter(connection.getOutputStream(), true /* autoFlush */); // read information from server String info; info = input.readLine(); System.out.println(info); boolean done = false; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String sInput; while(!done){ sInput = in.readLine(); out.println(sInput); if (sInput.equalsIgnoreCase("bye")) done = true; info = input.readLine(); System.out.println(info); } connection.close(); } catch(SecurityException e){ System.out.println("SecurityException when connecting Server!"); } catch(IOException e){ System.out.println("IOException when connecting Server!"); } }} 运行1 运行服务器java EchoServer12 运行 客户端java EchoClient1 server_hostname Swing GridBagLayout 布局的问题 谁能帮我在Jtextare上加一个滚动条啊? jacORB问题 搞了好几天都没有好 求助各位大侠 !! pipestream是什么流? charAt()方法的问题?? java进度条问题 关于java的动态时间问题 初学者提问请进:怎么把字节数组按十六进制格式输出 关于在applet中拖动图片闪动的情况 Graphics绘图环境的问题,急,在线等待! Visual J++6.0 事件问题?
不要乱下结论
import java.net.*;public class EchoServer1
{ public static void main(String[] args )
{ try
{
ServerSocket s = new ServerSocket(8500);
Socket incoming = s.accept( );
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */);out.println( "Hello! Enter BYE to exit." );boolean done = false;
while (!done)
{ String line = in.readLine();
if (line == null) done = true;
else
{ out.println("Echo: " + line);if (line.trim().equals("BYE"))
done = true;
}
}
incoming.close();
}
catch (Exception e)
{ System.out.println(e);
}
}
}
// EchoClient1.java
import java.io.*;
import java.net.*;public class EchoClient1 {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),8500);
}
else{
connection=new Socket(InetAddress.getByName(connectto),8500);
}
BufferedReader input=new BufferedReader(new InputStreamReader(connection.getInputStream()));
PrintWriter out = new PrintWriter(connection.getOutputStream(), true /* autoFlush */);
// read information from server
String info;
info = input.readLine();
System.out.println(info);
boolean done = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String sInput;
while(!done){
sInput = in.readLine();
out.println(sInput);
if (sInput.equalsIgnoreCase("bye")) done = true;
info = input.readLine();
System.out.println(info);
}
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}
} 运行
1 运行服务器
java EchoServer1
2 运行 客户端
java EchoClient1 server_hostname