Eclipse中用两个控制台测试网络通信程序

本文介绍了一个简单的聊天室程序实现,包括服务器端和客户端代码。服务器端接收客户端发送的消息并回显给客户端,客户端则发送消息到服务器并接收回显的消息。通过两个控制台窗口展示交互过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 服务器端:

//: JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
package foo;

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

public class Server {
	// Choose a port outside of the range 1-1024:
	public static final int PORT = 8080;

	public static void main(String[] args) throws IOException {
		ServerSocket s = new ServerSocket(PORT);
		System.out.println("Started: " + s);
		try {
			// Blocks until a connection occurs:
			Socket socket = s.accept();
			try {
				System.out.println("Connection accepted: " + socket);
				BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				// Output is automatically flushed
				// by PrintWriter:
				PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
						true);
				while (true) {
					String str = in.readLine();
					if (str.equals("END"))
						break;
					System.out.println("Echoing: " + str);
					out.println(str);
				}
				// Always close the two sockets...
			} finally {
				System.out.println("closing...");
				socket.close();
			}
		} finally {
			s.close();
		}
	}
}
客户端:

package foo;

//: JabberClient.java
// Very simple client that just sends
// lines to the server and reads lines
// that the server sends.
import java.net.*;
import java.io.*;

public class Client {
	public static void main(String[] args) throws IOException {
		// Passing null to getByName() produces the
		// special "Local Loopback" IP address, for542
		// testing on one machine w/o a network:
		InetAddress addr = InetAddress.getByName(null);
		// Alternatively, you can use
		// the address or name:
		// InetAddress addr =
		// InetAddress.getByName("127.0.0.1");
		// InetAddress addr =
		// InetAddress.getByName("localhost");
		System.out.println("addr = " + addr);
		Socket socket = new Socket(addr, Server.PORT);
		// Guard everything in a try-finally to make
		// sure that the socket is closed:
		try {
			System.out.println("socket = " + socket);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			// Output is automatically flushed
			// by PrintWriter:
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),
					true);
			for (int i = 0; i < 10; i++) {
				out.println("howdy " + i);
				String str = in.readLine();
				System.out.println(str);
			}
			out.println("END");
		} finally {
			System.out.println("closing...");
			socket.close();
		}
	}
}


运行时发现需要两个控制台Console来分别显示:点击 图中 open console  -- new  console view  就会出现两个控制台了



然后将控制台拖出,方便同时查看,然后在程序运行完成后击 display  selected console 来选择要显示的控制台视图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值