simpleclient.java

来自「JAVA程序设计课程中各章节的程序实例。」· Java 代码 · 共 54 行

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

public class SimpleClient
{
	public static void main(String args[]) throws IOException
	{
		int c;
		Socket s1;
		
		InputStream s1_In;
		DataInputStream din_s1;
		
		OutputStream s1_out;
		DataOutputStream dout_s1;
		
		BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
		s1 = new Socket(InetAddress.getLocalHost(),5432);

		String s1_recv = new String("");
		String s1_send = new String("");

		s1_out = s1.getOutputStream();
		dout_s1 = new DataOutputStream(s1_out);
		s1_In = s1.getInputStream();
		din_s1 = new DataInputStream(s1_In);
		
		while(!s1_send.equals("Bye") && !s1_recv.equals("Bye"))
		{
			try
			{
				s1_out = s1.getOutputStream();
				dout_s1 = new DataOutputStream(s1_out);
				s1_send = b.readLine();
				dout_s1.writeUTF(s1_send);
			
				s1_In = s1.getInputStream();
				din_s1 = new DataInputStream(s1_In);
				s1_recv = din_s1.readUTF();
				System.out.println("Server: " + s1_recv);
			}
			catch(IOException e)
			{
				System.out.println("exception caught");
			}
		}
		
		din_s1.close();
		dout_s1.close();
		s1.close();
		s1_In.close();
		s1_out.close();
	}
}

⌨️ 快捷键说明

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