程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java網絡編程-Java寫了個簡單的網絡編程程序,運行沒有結果,求解謝謝!

java網絡編程-Java寫了個簡單的網絡編程程序,運行沒有結果,求解謝謝!

編輯:編程綜合問答
Java寫了個簡單的網絡編程程序,運行沒有結果,求解謝謝!

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

xn6517
xn6517
qq_18295431
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved