• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

了解Socket技术

通过套接字技术(这是计算机之间的通信一种约定的一种方式),我们可以实现两台计算机之间的通信。Socket又翻译成socket,是操作系统底层提供的一种通信技术,它支持TCP和UDP。Java对socket的底层支持有完整的包,我们可以通过Java实现Socket通信。

要实现Socket通信,就要创建数据发送方和数据接收方,也就是客户端和服务器端。我们需要提前启动服务器等待客户端的连接,客户端只需要随时启动就可以连接到服务器!

//服务器端

公共静态void main(String[] args) {

try(server socket server=new server socket(8080)){//在端口8080上创建服务器。

System.out.println('等待客户端连接.');

socket socket=server . accept();//没有客户端连接时,线程会一直阻塞,直到有客户端连接。

System.out.println('客户端连接,IP地址为:' socket.getinet address()。gethostaddress()));

}catch (IOException e){

e . printstacktrace();

}

}

//客户端

公共静态void main(String[] args) {

try(Socket Socket=new Socket(' localhost ',8080)){

System.out.println('连接到服务器!');

}catch (IOException e){

System.out.println('服务器连接失败!');

e . printstacktrace();

}

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now