- public class TestDemo4 {
- public static void main(String[] args) throws Exception{
- new Thread(new Client()).start();
- new Thread(new Server()).start();
- }
- }
- class Server implements Runnable
- {
-
- public void run()
- {
- try {
- rever();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void rever() throws Exception
- {
- ServerSocket ss = new ServerSocket(10001);
-
- Socket s = ss.accept();
-
- InputStream is = s.getInputStream();
-
- int ch = -1;
-
- while((ch = is.read())!=-1)
- {
- System.out.print((char)ch);
- }
-
- /* OutputStream os = s.getOutputStream();
- s.shutdownOutput();
-
- String http = "这是服务器端协议";
- os.write(http.getBytes());*/
-
- }
- }
- class Client implements Runnable
- {
-
- public void run()
- {
- try {
- sender();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void sender() throws Exception
- {
- String str = "这是客户端的协议";
- InetAddress ia = InetAddress.getLocalHost();
- Socket s = new Socket(ia,10001);
-
- OutputStream os = s.getOutputStream();
-
- os.write(str.getBytes());
- //s.shutdownInput();
-
-
-
-
- InputStream is = s.getInputStream();
-
- int ch = -1;
- while((ch = is.read())!=-1)
- {
- System.out.print((char)ch);
- }
-
-
-
- }
- }
复制代码
这段代码为什么传递的数据是乱码?
|