很简单的 客户端 服务器程序,收发都有。就是不正常。 总是停在哪里。和Linux 没关系吧!Windows上没试public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4321);
Socket cs = ss.accept();
BufferedReader rcs = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter wcs = new PrintWriter(cs.getOutputStream()); String s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s); rcs.close();
wcs.close();
cs.close();
ss.close();
}
}public class Client {
public static void main(String[] args) throws IOException {
Socket ss = new Socket("localhost", 4321);
BufferedReader rss = new BufferedReader(new InputStreamReader(ss.getInputStream()));
PrintWriter wss = new PrintWriter(ss.getOutputStream()); wss.println("Why?");
String s;
s = rss.readLine();
System.out.println(s);
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println(s); rss.close();
wss.close();
ss.close(); }
}给点建议~~
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(4321);
Socket cs = ss.accept();
BufferedReader rcs = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter wcs = new PrintWriter(cs.getOutputStream()); String s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s); rcs.close();
wcs.close();
cs.close();
ss.close();
}
}public class Client {
public static void main(String[] args) throws IOException {
Socket ss = new Socket("localhost", 4321);
BufferedReader rss = new BufferedReader(new InputStreamReader(ss.getInputStream()));
PrintWriter wss = new PrintWriter(ss.getOutputStream()); wss.println("Why?");
String s;
s = rss.readLine();
System.out.println(s);
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println("1");
wss.println(s);
s = rss.readLine();
System.out.println(s); rss.close();
wss.close();
ss.close(); }
}给点建议~~
PrintWriter wss = new PrintWriter(ss.getOutputStream(),true);
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);
s = rcs.readLine();
System.out.println(s);
wcs.println(s);建议循环。