BungeeCord not reachable after enabling ServerSocket - java

I'm working on an web based API for a BungeeCord Server but after opening the ServerSocket on Port 8082 the BungeeCord on Port 25565 isn't available furthermore.
This class is opening the ServerSocket:
package de.pardrox.bungeeapi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class HTTP {
static router router = new router();
public static void main(int args) {
try {
int port = args;
#SuppressWarnings("resource")
ServerSocket apiweb = new ServerSocket(port);
for (;;) {
Socket client = apiweb.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream());
out.print("HTTP/1.1 200 \r\n");
out.print("Content-Type: text/plain\r\n");
out.print("Connection: close\r\n");
out.print("\r\n");
String line;
InetAddress ip_client = client.getInetAddress();
main.syslog("Request of client "+ip_client.toString());
while ((line = in.readLine()) != null) {
if (line.length() == 0)
break;
if(line.toLowerCase().contains("GET".toLowerCase()))
{
String url = line.replace("GET ", "").replace(" HTTP/1.1", "");
out.print(router.get(url));
}
}
out.close();
in.close();
client.close();
}
}
catch (Exception e) {
System.err.println(e);
System.err.println("Call HTTP(<port>)");
}
}
}
Does anyone have an idea why opening the ServerSocket seems to close the Socket of the Gameserver? Eclipse doesn't find any error and the gameserver itself seems to run fine. The API is reachable also without any trouble.
For completeness:
I've startet the socket class with HTTP.main(8082);

I think there's a mistake at for (;;).
This will create an infinite loop that will run for ever....
Maybe this will cause the Main Thread of BungeeCord server to stop responding.
Try removing the for (;;) and using this code below instead of just running the code in the Default BungeeCord Thread. Since BungeeCord doesn't allows you to create custom Threads, the only way to do this is using the Scheduler and running a Runnable asynchronous.
ProxyServer.getInstance().getScheduler().runAsync(yourPluginHere, new Runnable() {
#Override
public void run() {
// Put your code here
}
});

Related

Run running TCP and UDP server at the same time

I have a network programming topic that requires file transfer using TCP and UDP. If TCP send fails then UDP will be executed. I have built each part but I am not sure how can I run the server of TCP and UDP at the same time to be able to receive data of both protocols (my problem is starting 2 server in the master server because I have as the interface). Hope everybody help please .
In one case you need to open a ServerSocket, in the other case a DatagramSocket. It should be possible to open them in parallel, which means you can run both your implementations in parallel running on different threads.
If you are suppose to run the TCP and the UDP Server on the same machine, you will need to use different ports. Then you can start up two different thread, one for each server:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
public class TcpUdpServer {
private final static int UDP_PORT = 8100;
private final static int TCP_PORT = 8200;
public static void main(String[] args) {
new Thread(() -> executeTcpServer()).start();
new Thread(() -> executeUdpServer()).start();
}
public static void executeTcpServer() {
try (ServerSocket serverSocket = new ServerSocket(TCP_PORT)) {
while (true) {
System.out.println("waiting for TCP connection...");
// Blocks until a connection is made
final Socket socket = serverSocket.accept();
final InputStream inputStream = socket.getInputStream();
String text = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
System.out.println(text);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void executeUdpServer() {
try (DatagramSocket socket = new DatagramSocket(UDP_PORT)) {
while (true) {
byte[] packetBuffer = new byte[2024];
final DatagramPacket packet = new DatagramPacket(packetBuffer, packetBuffer.length);
System.out.println("waiting for UDP packet...");
// Blocks until a packet is received
socket.receive(packet);
final String receivedPacket = new String(packet.getData()).trim();
System.out.println(receivedPacket);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

Why the server stops running when I close the Client?

I am writing my first Client-Server program in Java using Sockets. I am using Eclipse as the IDE. When I am testing the communication between both programs (server and client) I run first the server using the command prompt and then I run the client in Eclipse. Everything works fine, I can read from and write to the socket, however, when I close the client program in Eclipse, the server program closes too. Why is this happening? The server is supposed to be running by itself in the command prompt, it is not dependent on a client.
Also I would like to know if there is any possibility I can run both programs in Eclipse instead of opening the server in the command prompt first.
Here is my code for both programs:
Server:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerPrg {
public static void main(String[] args) {
System.out.println("Server is running.....");
try {
ServerSocket socketSer = new ServerSocket(4444);
Socket clientSocket = socketSer.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = (new BufferedReader (new InputStreamReader(clientSocket.getInputStream())));
BufferedReader stdIn = (new BufferedReader (new InputStreamReader(System.in)));
System.out.println("Client: " + in.readLine());
String input ;
while((input = stdIn.readLine()) != null)
{ out.println(input);
System.out.println("Client: " + in.readLine());
}
}
catch (Exception e) {System.out.println("CAN'T CREATE SERVERSOCKET. PROBABLY THE PORT IS BEING USED " + e);}
} //end main
} //end public class
Client:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientPrg {
public static void main(String[] args) {
int portNumber = 4444;
try {
Socket clientSocket = new Socket("127.0.0.1", portNumber);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String input;
while ((input = stdIn.readLine()) != null)
{
out.println(input);
System.out.println("Server: " + in.readLine());
}
} catch(Exception e)
{
System.out.println(e);
System.out.println("CAN'T CONNECT TO THE SERVER");
}
} //end main
} // end public class
Your server lacks a loop around accepting client sockets.
This means that after your client socket is accepted, it will exit because there is no flow control element that will have it attempt to accept a second client socket.
A simple loop around accepting client sockets is probably not exactly what you want. That is because there will be only one Thread in the solution, which means that while a client is being handled, other clients won't be able to be accepted.
There are many ways to handle the situation above. One of the simplest is to create a thread for every accepted client to handle the client's communications. While this is initially simple, it does not scale very well. With a large number of clients, the thread count will rise, and most computers can handle many more network connections than threads.
The scope of talking about services that scale well is far to big to address here; but, after you get familiar with one thread per client processing, start looking at Java NIO.

Java EchoTCPServer - Send to all clients

Here's my code:
SERVER:
package server;
public class Main {
public static void main(String args[]) {
new EchoServer(9000);
}
}
+
package server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private ServerSocket server;
public EchoServer(int port) {
try {
server = new ServerSocket(port);
while (true) {
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(in.readLine() + " | MOD");
socket.close();
}
} catch(Exception err) {
err.printStackTrace();
}
}
}
CLIENT:
package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
try {
while (true) {
Socket socket = new Socket("localhost", 9000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
out.println(input.readLine());
System.out.println(in.readLine());
socket.close();
}
} catch (Exception err) {
System.out.println(err);
}
}
}
It works all as it should, except that I want when the server sends the "message" + " | MOD" to the client, I want the server to send that to all clients, how can I do that?
I am new to Java, but not to coding so please help me if I've done some wrong stuff that can be done easier or better.
Please help.
Thanks alot.
What you can do is save the client sockets in an array, and then use a for loop to send to each socket.
First, declare your clientSocket array; note that 5 is just an arbitrary size used for testing. Also, declare a counter int.
public Socket clientSocket[] = new Socket[5];
public int intLastSocket = 0;
// this should be placed where you're waiting to accept connections
while (true) {
printTCP("Ready to accept welcome socket");
clientSocket[intLastSocket] = welcomeSocket.accept();
intLastSocket++;
}
// on the server, call this to send. s is a reference to the server object
public void sendToAllTCP(TCPServer s, String message) {
for (Socket z : s.clientSocket) {
if (z != null) {
PrintStream outToClient = null;
try {
outToClient = new PrintStream(z.getOutputStream());
outToClient.println(message);
} catch (IOException e) {
TCPServer.printTCP("Caught an IO exception trying "
+ "to send to TCP connections");
e.printStackTrace();
}
}
}
}
IN YOUR CODE:
package com.murplyx.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private ServerSocket server;
// use the clientSocket Array to save each new connection
public Socket clientSocket[] = new Socket[5];
public EchoServer(int port) {
try {
server = new ServerSocket(port);
// this should be placed where you're waiting to accept connections
while (true) {
System.out.println("Ready to accept welcome socket");
clientSocket[intLastSocket] = server.accept();
intLastSocket++;
//send your message here, note that putting
//it here will send it each time u add a new connection
sendToAllTCP(/*the message you want to send */);
}
} catch(Exception err) {
err.printStackTrace();
}
}
public void sendToAllTCP(String message) {
// this is an enchanced for loop, i don't know if it's in other languages
// but in java it's supposed to let you loop through
//each object in any iterable list
// such as array, arraylist, linkedlist, etc
for (Socket z : clientSocket) {
if (z != null) {
//basically this chunk of code declares output and input streams
//for each socket in your array of saved sockets
PrintStream outToClient = null;
try {
outToClient = new PrintStream(z.getOutputStream());
outToClient.println(message);
} catch (IOException e) {
System.out.println("Caught an IO exception trying "
+ "to send to TCP connections");
e.printStackTrace();
}
}
}
}
}
Depending on when you want to send your message, you can use the console and sys.in to send it. For example, if you read a line from sys.in and it .equals("sendMsg"), then you can call sendToAllTCP(yourmessage)
You should take a look at multiThreaded chat Server. Each client wich connects gets it's own thread.
Here is the perfect answer to your question:
multithread client-server chat, using sockets
Good luck mate!

Server Socket won't accept() local application handshake in Java

I am trying to connect a client application to my code through port 8000 on my pc offline. I am using the ServerSocket library to connect using the below code. The client's application sends XML messages across the port and I need to connect, interpret and respond. (Please bear in mind that I haven't coded interpret/respond yet when reading the below).
Every time I try to send a message from the client application (when running the debug feature in eclipse), the code gets to the serverSocket.accept() method which should be the 'handshake' between client and server and can't go any further. I am unable to change the client application obviously so need to figure out why it's not accepting the connection.
package connect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class PortListener2 {
public static void main(String[] args){
System.out.println("> Start");
int portNumber = 8000;
try (
ServerSocket serverSocket =
new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received: "+inputLine);
out.println(inputLine);
if (inputLine.equals("exit")){
break;
}
}
} catch (Exception e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
System.out.println("Disconnected");
}
}
}

Handle streams with multiple clients?

basically what i want to do is develop a chat program(something between an instant messenger and IRC) to improve my java skills.
But I ran into 1 big problem so far: I have no idea how to set up streams properly if there is more than one client. 1:1 chat between the client and the server works easily, but I just don't know what todo so more than 1 client can be with the server in the same chat.
This is what I got, but I doubt its going to be very helpful, since it is just 1 permanent stream to and from the server.
private void connect() throws IOException {
showMessage("Trying to connect \n");
connection = new Socket(InetAddress.getByName(serverIP),27499);
showMessage("connected to "+connection.getInetAddress().getHostName());
}
private void streams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n streams working");
}
To read from multiple streams in one program, you're going to have to use multithreading. Because reading from streams is synchronous, you'll need to read from one stream for each thread. See the java tutorial on threads for more info on multithreading.
I've done this several times with ServerSocket(int port) and Socket ServerSocket.accept(). This can be pretty simple by having it listen to the one port you want your chat server client listening on. The main thread will block waiting for the next client to connect, then return the Socket object to that specific client. Usually you'll want to put them in a list to generically handle n-number of clients.
And, yes, you will probably want to make sure each Socket is in a different thread, but that's entirely up to you as the programmer.
Remember, there is no need to re-direct to another port on the server, by virtue of the client using a different source port, the unique 5-tuple (SrcIP, SrcPort, DstIP, DstPort, TCP/UDP/other IP protocol) will allow the one server port to be re-used. Hence why we all use stackoverflow.com port 80.
Happy Coding.
Made something like that a few months back. basically I used a separate ServerSocket and Thread per client server side. When client connects you register that port's input and output streams to a fixed pool and block until input is sent. then you copy the input to each of the other clients and send. here is a basic program run from command line:
Server code:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ChatServer {
static int PORT_NUMBER = 2012;
public static void main(String[] args) throws IOException {
while (true) {
try (ServerSocket ss = new ServerSocket(PORT_NUMBER)) {
System.out.println("Server waiting #" + ss.getInetAddress());
Socket s = ss.accept();
System.out.println("connection from:" + s.getInetAddress());
new Worker(s).start();
}
}
}
static class Worker extends Thread {
final static ArrayList<PrintStream> os = new ArrayList(10);
Socket clientSocket;
BufferedReader fromClient;
public Worker(Socket clientSocket) throws IOException {
this.clientSocket = clientSocket;
PrintStream toClient=new PrintStream(new BufferedOutputStream(this.clientSocket.getOutputStream()));
toClient.println("connected to server");
os.add(toClient);
fromClient = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
}
#Override
public void run() {
while (true) {
try {
String message = fromClient.readLine();
synchronized (os) {
for (PrintStream toClient : os) {
toClient.println(message);
toClient.flush();
}
}
} catch (IOException ex) {
//user discnnected
try {
clientSocket.close();
} catch (IOException ex1) {
}
}
}
}
}
}
Client code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
final BufferedReader fromUser = new BufferedReader(new InputStreamReader(System.in));
PrintStream toUser = System.out;
BufferedReader fromServer;
final PrintStream toServer;
Socket s = null;
System.out.println("Server IP Address?");
String host;
String port = "";
host = fromUser.readLine();
System.out.println("Server Port Number?");
port = fromUser.readLine();
s = new Socket(host, Integer.valueOf(port));
int read;
char[] buffer = new char[1024];
fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
toServer = new PrintStream(s.getOutputStream());
new Thread() {
#Override
public void run() {
while (true) {
try {
toServer.println(">>>" + fromUser.readLine());
toServer.flush();
} catch (IOException ex) {
System.err.println(ex);
}
}
}
}.start();
while (true) {
while ((read = fromServer.read(buffer)) != -1) {
toUser.print(String.valueOf(buffer, 0, read));
}
toUser.flush();
}
}
}

Categories