Java Socket 编程注意 PrintWriter

利用Java进行Socket编程时,一般的,输出流,我们会选择PrintWriter。当选择PrintWriter时,要注意它的两个方法:

write()  和 println();

假如你写了个很简单的C/S通信程序,当使用Write方法时,你可能在Client端输完一行了,打了回车,但Server端还是看不到Client端输入的信息。但如果你使用Println()就不会。为什么呢?当查看Java API的时候发现:

                  使用println()时,当你输入行分隔符(如回车)时,程序会帮你终止当前行,而write则不会。这时候,write()跟println()的区别 和 print() 跟 println() 的区别是一样的。



  1. input = new BufferedReader(new InputStreamReader(  
  2.             socket.getInputStream()));  
  3.     output = new PrintWriter(socket.getOutputStream(),true);  
  4.   
  5.     scn = new Scanner(System.in);  
  6.     String outStr, inStr;  
  7.     while ((inStr = input.readLine()) != null) {  
  8.         if ("Bye".equals(inStr)) {  
  9.             break;  
  10.         }  
  11.         System.out.println("Client:" + inStr);  
  12.         outStr = scn.nextLine();  
  13.         output.println(outStr);  
  14.   
  15.     }  
		input = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			output = new PrintWriter(socket.getOutputStream(),true);

			scn = new Scanner(System.in);
			String outStr, inStr;
			while ((inStr = input.readLine()) != null) {
				if ("Bye".equals(inStr)) {
					break;
				}
				System.out.println("Client:" + inStr);
				outStr = scn.nextLine();
				output.println(outStr);

			}


在Dos控制台编译运行的时候,要注意,如果遇到以下情况,最好去检查下环境变量有没有配好:




比较常见的是:在ClassPath中少了个分号。如果少了个分号,就会出现上面的错误。




还有一点值得注意:当使用SSL对socket进行加密是,证书文件应该跟.class文件放在同一个目录下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.io.*; import java.net.*; import java.util.*; import java.lang.*; public class Server extends ServerSocket { private static ArrayList User_List = new ArrayList(); private static ArrayList Threader = new ArrayList(); private static LinkedList Message_Array = new LinkedList(); private static int Thread_Counter = 0; private static boolean isClear = true; protected static final int SERVER_PORT = 10000; protected FileOutputStream LOG_FILE = new FileOutputStream( "d:/connect.log", true); public Server() throws FileNotFoundException, IOException { super(SERVER_PORT); // append connection log // Calendar now = Calendar.getInstance(); // String str = "[" + now.getTime().toString() + // "] Accepted a connection"; // byte[] tmp = str.getBytes(); // LOG_FILE.write(tmp); try { Socket socket = accept(); while (true) { new ServerReaderThread(socket); new ServerWriterThread(socket); } } finally { close(); } } public static void main(String[] args) throws IOException { new Server(); } // --- CreateServerThread class ServerReaderThread extends Thread { private Socket client; private BufferedReader in; private PrintWriter out; private String Username; public ServerReaderThread(Socket s) throws IOException { client = s; in = new BufferedReader(new InputStreamReader(client .getInputStream())); out = new PrintWriter(client.getOutputStream(), true); start(); } public void run() { try { int flag = 0; Thread_Counter++; String line = in.readLine(); while (!line.equals("bye")) { out.println(line); line = in.readLine(); } out.println("--- See you, bye! ---"); // System.out.println("--- See you, bye! ---"); client.close(); } catch (IOException e) { } finally { try { client.close(); } catch (IOException e) { } Thread_Counter--; } } } // --- CreateServerThread class ServerWriterThread extends Thread { priva

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值