码迷,mamicode.com
首页 > 其他好文 > 详细

创建端口

时间:2017-07-18 10:10:42      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:unknown   eth   client   oca   创建   null   close   string   output   

import java.io.BufferedReader;
import java.io.*;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;


public class Client {
	private static PrintWriter pw=null;
	private static BufferedReader br=null;
	private static Socket s;
	private static ServerSocket ss;
	static Scanner scanner=new Scanner(System.in);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Socket s=new Socket(InetAddress.getLocalHost(),5500);
			pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			while(true){
				System.out.println("Client端请输入:");
				String str = scanner.next();
				pw.println(str);
				pw.flush();
				String string=br.readLine();
				System.out.println("Client读到:"+string);
				if(str.equals("exit")){
					break;
				}
			} 
		}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		try {
			br.close();
			pw.close();
		} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
		}

  

创建端口

标签:unknown   eth   client   oca   创建   null   close   string   output   

原文地址:http://www.cnblogs.com/adada/p/7198644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!