欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

socketclient.java.bak

有关java的源程序,为讲授java程序设计课程使用
BAK
字号:
import java.io.*;
import java.net.*;
public class SocketClient
{   public static void main(String args[])
	{
	String HostName="127.0.0.1";
	Socket s=null;
	int port=1000;
	DataInputStream sin=null;
	PrintStream sout=null;
	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.err.println("Don't know about host"+args[0]);
			}
		catch(IOException e)
			{ System.err.println("Canot get I/O for the Connect to"+HostName);
			}
		System.out.println("Connected to"+s.getInetAddress()+":"+s.getPort());
		String text;
		try
		{
			while(true)
			{   System.out.print(":");
				System.out.flush();
				if((text =dis.readLine())!=null)
				{   sout.println(text);
					if((text =sin.readLine())!=null)
						System.out.println(text);
					else
					{ System.out.println("Connection closed by server");
					   break;
					}
				}
				else
					 break;
			}
			sin.close();
			sout.close();
			s.close();
		}
		catch(IOException e)
			{ System.err.println("I/O failed on the connection to"+HostName);
			}
	}
}

⌨️ 快捷键说明

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