client.java

来自「Socket简单服务器和客户端其他人不需帐号就可自由下载此源码」· Java 代码 · 共 39 行

JAVA
39
字号
package test;

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

public class Client {      
       public static void main(String args[])   {
                try{			
			if (args.length != 1){
				System.out.println("USAGE: java Client servername");				
				return;
			}
			String  connectto= args[0];
                        Socket connection;
			// connect to server
			if(connectto.equals("localhost")){
				connection=new Socket(InetAddress.getLocalHost(),5000);

			}
			else{
				connection=new Socket(InetAddress.getByName(connectto),5000);				
			}			
			DataInputStream  input=new DataInputStream(connection.getInputStream());                       

			// read information from server                        
                        String info;
                        info = input.readUTF();
                        System.out.println(info);
			connection.close();                                                               
		}
		catch(SecurityException e){
			System.out.println("SecurityException when connecting Server!");		
		}		
		catch(IOException e){
			System.out.println("IOException when connecting Server!");   
		}
      	}

}

⌨️ 快捷键说明

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