如何使服务器允许连接到Java中的 Socket 6123?
套接字连接意味着两台机器有关于彼此的网络位置(IP 地址)和 TCP 端口的信息。 Java.net.Socket 类代表一个 Socket。下面我们来看看连接socket 6123的方法。
方法:
- 创建一个 Socket 类的对象并将 6123 作为参数传递。
- 使用 ServerSocket 类的accept()方法接受连接。
- 使用 Socket 类的getInetAddress()方法和 InetAddress 类的getHostAddress()获取连接地址。
服务器代码:
Java
// Make a server to allow the connection to the socket 6123
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) { check(); }
private static void check()
{
try {
// Creating object of ServerSocket class
ServerSocket connection
= new ServerSocket(6123);
while (true) {
System.out.println("Listening");
// Creating object of Socket class
Socket socket = connection.accept();
// Creating object of InetAddress class
InetAddress address
= socket.getInetAddress();
System.out.println(
"Connection made to "
+ address.getHostAddress());
pause(1000);
// close the socket
socket.close();
}
}
catch (IOException e) {
System.out.println("Exception detected: " + e);
}
}
private static void pause(int ms)
{
try {
Thread.sleep(ms);
}
catch (InterruptedException e) {
}
}
}
Java
// A Java program for a Client
import java.net.*;
import java.io.*;
public class Client {
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(
socket.getOutputStream());
}
catch (UnknownHostException u) {
System.out.println(u);
}
catch (IOException i) {
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over")) {
try {
line = input.readLine();
out.writeUTF(line);
}
catch (IOException i) {
System.out.println(i);
}
}
// close the connection
try {
input.close();
out.close();
socket.close();
}
catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 6123);
}
}
输出
客户代码:
Java
// A Java program for a Client
import java.net.*;
import java.io.*;
public class Client {
// initialize socket and input output streams
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream out = null;
// constructor to put ip address and port
public Client(String address, int port)
{
// establish a connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(
socket.getOutputStream());
}
catch (UnknownHostException u) {
System.out.println(u);
}
catch (IOException i) {
System.out.println(i);
}
// string to read message from input
String line = "";
// keep reading until "Over" is input
while (!line.equals("Over")) {
try {
line = input.readLine();
out.writeUTF(line);
}
catch (IOException i) {
System.out.println(i);
}
}
// close the connection
try {
input.close();
out.close();
socket.close();
}
catch (IOException i) {
System.out.println(i);
}
}
public static void main(String args[])
{
Client client = new Client("127.0.0.1", 6123);
}
}
输出