appclienttwo.java

来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 58 行

JAVA
58
字号
import java.net.*;
import java.io.*;

public class AppClientTwo
{public static void main (String args[])
        { 
	Socket s=null;
	int port=1001;
	DataInputStream sin=null;
	PrintStream sout=null;
    String HostName="192.168.0.9";
	DataInputStream dis=new DataInputStream(System.in);
	try
		{	s=new Socket(HostName,port);
			sin=new DataInputStream(s.getInputStream());	
			sout=new PrintStream(s.getOutputStream());
		}
		catch(UnknownHostException e)
                        {
			System.out.println("\n"+"Don't know about host"+HostName);
			}
		catch(IOException e)
			{ System.out.println("\n"+"Canot get I/O for the Connect to"+HostName);
			}
		System.out.println("Connected to "+s.getInetAddress()+":"+s.getPort());

			String acceptString;
			try
			{ while (true)
				{System.out.print(":");
				System.out.flush();
				if((acceptString = dis.readLine())!=null) 
					{sout.println(acceptString); 
					if((acceptString =sin.readLine())!=null)
						System.out.println("\n"+acceptString);
					else
						{System.out.println("\n"+"Closed by Server");
						 break;
						}
					}
				else	
					{try
						{sin.close();
						 sout.close();
						 s.close();
						}
					 catch(IOException e)
						{System.out.println("\n"+"I/O failed on the connection to"+HostName);
						}
					}
				}
			}
			catch(IOException e)
				{ System.out.println("\n"+"I/O failed on the connection to"+HostName);
				}
	}
}

⌨️ 快捷键说明

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