⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclient.java

📁 Java面试大全.rar Java面试大全.rar
💻 JAVA
字号:
//客户端程序的开发原理:
//使用Socket对网络上某一个服务器的某一个端口发出连接请求,一旦连接成功,打开会话;
//会话完成后,关闭Socket。

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 + -