本文介绍了任何人都可以帮我用swing GUI创建套接字编程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经完成了一个套接字程序,其中多个客户端向server1发送数据,server1将数据转发到server2。现在我想用Swing GUI做这个程序。任何人都可以帮我创建它吗?
我在下面提交我的代码==
我尝试了什么:
客户
I already done a socket program where multiple client send data to server1 and server1 forward the data to server2. Now I want to do this program with Swing GUI. Can anyone help me how to create it?
I submit my code below ==
What I have tried:
Client
import java.net.*;
import java.io.*;
public class JavaClientApp {
public static void main(String[] args) throws IOException {
// Setting server hostname 127.0.0.2
String hostName = "127.0.0.2";
// Setting server port number 5056
int portNumber = Integer.parseInt("5056");
Boolean done = false;
while (done == false) {
// Instantinate the buffer reader object to read data from stdin
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type in your message /> ");
// Retrieve each line from user input
String userInput = stdIn.readLine();
if (userInput.equals("Quit") || userInput.equals("Exit")) {
done = true; break;
}
// Print out `Sending a message...` string
System.out.println("Sending a message...");
Socket clientSocket = new Socket(hostName, portNumber);
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter outputStream = new PrintWriter(clientSocket.getOutputStream(), true);
outputStream.println(String.format(userInput));
String msg_buf = inputStream.readLine();
System.out.println(msg_buf);
clientSocket.close();
}
}
}
server1
server1
import java.net.*;
import java.io.*;
public class JavaMTServer {
public static void main(String[] args) throws IOException {
// TODO code application logic here
// Setting server hostname 127.0.0.2
String hostName = "127.0.0.2";
// Setting server port number 5058
int portNumber = Integer.parseInt("5058");
@SuppressWarnings("resource")
ServerSocket srvSocket = new ServerSocket(5056);
while (true) {
Socket clientSocket = srvSocket.accept();
Thread cliSockThread = new Thread() {
public void run() {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter outputStream = new PrintWriter(clientSocket.getOutputStream(), true);
String msg_buf = inputStream.readLine();
System.out.println(msg_buf);
Socket clientSTSocket = new Socket(hostName, portNumber);
BufferedReader inputStream2 = new BufferedReader(
new InputStreamReader(clientSTSocket.getInputStream()));
PrintWriter outputStream2 = new PrintWriter(clientSTSocket.getOutputStream(), true);
outputStream2.println(msg_buf);
String msg_buf2 = inputStream2.readLine();
System.out.println(msg_buf2);
outputStream.println(msg_buf2);
clientSTSocket.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Server Down Trying to reconnect");
// Logger.getLogger(JavaMTServer.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inputStream.close();
} catch (IOException ex) {
// Logger.getLogger(JavaMTServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
cliSockThread.start();
}
//srvSocket.close();
}
}
server2
server2
import java.net.*;
import java.io.*;
public class JavaSTServer {
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
ServerSocket srvSocket = new ServerSocket(5056);
while (true) {
Socket clientSocket = srvSocket.accept();
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter outputStream = new PrintWriter(clientSocket.getOutputStream(), true);
String msg_buf = "\0";
while ((msg_buf = inputStream.readLine()) != null) {
System.out.println(msg_buf);
outputStream.println("Echo: " + msg_buf);
}
clientSocket.close();
}
}
}
推荐答案
这篇关于任何人都可以帮我用swing GUI创建套接字编程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!