tcpclient.java

来自「java写的一个Tcp socket 一个client和server」· Java 代码 · 共 71 行

JAVA
71
字号
package com.demo.struts.else_test;

import java.io.*;
import java.net.*;

public class TcpClient {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
	
		Socket soc = null;
		InputStream is = null;
		OutputStream os = null;
		DataInputStream in = null;
		PrintStream  out = null;
		
		String strin = null;
		
		try{
			soc = new Socket("localhost",8000);
			
			System.out.println("Conceting to the server..........");
			
			is = soc.getInputStream();
			
			os = soc.getOutputStream();
			
			in = new DataInputStream(is);
			
			out = new PrintStream(os);
			
			strin = in.readLine();
			
			System.out.println("Server said:" + strin);
			
			byte[] bmsg = new byte[20];
			
			System.in.read(bmsg);
			
			String msg = new String(bmsg, 0);
			
			msg = msg.trim();
			
			while(!msg.equals("quit")){
				out.println(msg);
				
				System.in.read(bmsg);
				msg = new String(bmsg, 0);
				msg = msg.trim();
			}
			out.println(msg);
			
		}catch(Exception e){
			System.out.println("error:" + e);
		}
		finally{
			is.close();
			os.close();
			soc.close();
			System.exit(0);
			
			
		}

	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?