给您写了个测试程序:
服务器端:
public class Trail {
public static void main(String[] args) throws IOException {
ServerSocket sss = new ServerSocket(7777);
Socket ss = sss.accept();
BufferedReader br = new BufferedReader( new InputStreamReader(ss.getInputStream()) );
System.out.println(br.readLine());
br.close();
ss.close();
sss.close();
}
}
客户端:
public class Testext {
public static void main(String[] args) throws Exception {
Socket ssc = new Socket("127.0.0.1", 7777);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ssc.getOutputStream()) );
bw.write("黑马程序员");
bw.flush();
bw.close();
ssc.close();
}
}
其实就是用InputStreamReader 和 OutputStreamWriter 将getInputStream() 和 getOutputStream() 转换成字符流!
再用BufferedReader 和 BufferedWriter包一层可以提高读写效率。
|