import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Kehuduan {
public static void main(String[] args) {
Socket s;
try {
s = new Socket("127.0.0.1", 8888);
OutputStream out = s.getOutputStream();
PrintStream pw = new PrintStream(out);
pw.println("嘿嘿收到了嗎?");
pw.println("收到了回復!");
InputStream in = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
char[] c = new char[2];
int len = -1;
while((len = br.read(c)) != -1){
sb.append(c, 0, len);
}
System.out.println(sb);
br.close();
in.close();
pw.close();
out.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Fuwuqi {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
System.out.println("服務器已經啟動,正在等待連接。。。");
Socket s = ss.accept();
InputStream in = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
char[] c = new char[2];
int len = -1;
while((len = br.read(c)) != -1){
sb.append(c, 0, len);
}
System.out.println(sb);
OutputStream out = s.getOutputStream();
PrintWriter print = new PrintWriter(out);
print.println("嘿嘿");
print.println("嘻嘻");
print.close();
out.close();
br.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
把int len =-1換成0