是否可以在同一台机器上运行套接字服务器和套接字客户端? [英] Is it possible to run a socket server and socket client on the same machine?
问题描述
在java中,可以创建套接字服务器和套接字客户机,是否可以让套接字服务器的一个实例运行,以及在同一台机器上从套接字服务器接收数据的套接字/服务器客户机? / p>
例如套接字服务器在端口60010
上运行,并且套接字客户端在通过套接字连接到该端口的同一台机器上运行,或者我需要新机器并将其添加到我的网络?如果它有一个在TCP / IP层上运行的唯一IP地址和端口号。
这里有一个简单的可运行示例你开始了。它启动两个线程,一个使用 ServerSocket
,另一个使用 Socket
连接。一个连续发送字符串,另一个打印它们。
你应该可以直接运行这个类。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketTest {
public static void main(String [] args)throws IOException {
startServer();
startSender();
}
public static void startSender(){
(new Thread(){
@Override
public void run(){
try {
Socket s = new Socket(localhost,60010);
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
while(true){
out.write(Hello World!);
out.newLine();
out.flush();
Thread.sleep(200);
}
} catch(UnknownHostException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
}
})。
}
public static void startServer(){
(new Thread(){
@Override
public void run(){
ServerSocket ss;
try {
ss = new ServerSocket(60010);
Socket s = ss.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String line = null;
while((line = in.readLine())!= null){
System.out.println(line);
}
} catch(IOException e){
e.printStackTrace();
}
}
} )。开始();
}
}
In java it is possible to create a socket server and a socket client, is it possible to have an instance of the socket server running and a socket/server client that is receiving data from the socket server on the same machine?
e.g the socket server runs on port 60010 and the socket client is running on the same machine connecting to that port through a socket or will I need to by a new machine and add it to my network? If it has a unique IP Address and port number running on the TCP/IP layer.
Here's a simple runnable example to get you started. It starts two threads, one with a ServerSocket
and one which makes a Socket
connection. One continuously sends strings and the other prints them.
You should simply be able to run this class as-is.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketTest {
public static void main(String[] args) throws IOException {
startServer();
startSender();
}
public static void startSender() {
(new Thread() {
@Override
public void run() {
try {
Socket s = new Socket("localhost", 60010);
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
while (true) {
out.write("Hello World!");
out.newLine();
out.flush();
Thread.sleep(200);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static void startServer() {
(new Thread() {
@Override
public void run() {
ServerSocket ss;
try {
ss = new ServerSocket(60010);
Socket s = ss.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
这篇关于是否可以在同一台机器上运行套接字服务器和套接字客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!