为了账号安全,请及时绑定邮箱和手机立即绑定

局域网象棋 Java-02-S/C模式设计服务器与客户端

标签:
Java

如何理解Socket

打个比方,我们想去一个游乐园,我们需要游乐场的地址,而且还需要买好票,在营业时间内才能进去,所以我们在使用 Socket 的时候,需要一个 IP 地址和一个端口号,只有当这个 IP 地址和端口号开放的时候,我们才能去连接它。
于是我们需要创建一个 Socket 连接,先看看官方文档:

public Socket(String host,
int port)
throws UnknownHostException,
IOException
Creates a stream socket and connects it to the specified port number on the named host.
If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName(null). In other words, it is equivalent to specifying an address of the loopback interface.
If the application has specified a server socket factory, that factory’s createSocketImpl method is called to create the actual socket implementation. Otherwise a “plain” socket is created.
If there is a security manager, its checkConnect method is called with the host address and port as its arguments. This could result in a SecurityException.
Parameters:
host - the host name, or null for the loopback address.
port - the port number.
Throws:
UnknownHostException - if the IP address of the host could not be determined.
IOException - if an I/O error occurs when creating the socket.
SecurityException - if a security manager exists and its checkConnect method doesn’t allow the operation.
IllegalArgumentException - if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive.
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, SocketImplFactory.createSocketImpl(), SecurityManager.checkConnect(java.lang.String, int)

This class implements client sockets (also called just “sockets”). A socket is an endpoint for communication between two machines.
The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

翻译:
该类实现客户端套接字(也称为“套接字”)。套接字是两台机器之间通信的端点。套接字的实际工作由SocketImpl类的实例执行。通过更改创建套接字实现的套接字工厂,应用程序可以将自身配置为创建适合本地防火墙的套接字。

我的实现方式如下:

package chessc;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
public class ServerThread extends Thread
{
	Server father; //声明Server的引用
	ServerSocket ss;//声明ServerSocket的引用
	boolean flag=true;
	public ServerThread(Server father)
	{//构造器
		this.father=father;
		ss=father.ss;
	}
	public void run()
	{
		while(flag)
		{
			try
			{
				Socket sc=ss.accept();//等待客户端连接
				ServerAgentThread sat=new ServerAgentThread(father,sc);
				sat.start();//创建并启动服务器代理线程
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}
ss=new ServerSocket(port);//创建ServerSocket对象
st=new ServerThread(this);//创建服务器线程
st.start();//启动服务器线程
sc=new Socket(this.jtfHost.getText().trim(),port);//创建Socket对象
cat=new ClientAgentThread(this);//创建客户端代理线程	
cat.start();//启动客户端代理线程

如果游乐场只有一个检票入口,那一定很慢

为了加快检票进度,往往都会增加好几个通道,让很多人同时可以检票,在系统里,这就是新建一个“线程”,来实现并行。于是我在代码里的实现方式是,有新的客户端连接,就新建一个线程来接收消息。

Socket sc=ss.accept();//等待客户端连接
ServerAgentThread sat=new ServerAgentThread(father,sc);
sat.start();//创建并启动服务器代理线程
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
30
获赞与收藏
154

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消