No Action works after Enabling ServerSocket in JAVA - java

I have created a Server Socket and enabled it to listen to incoming streams.But after enabling the connection it should display a dialog Box showing message "Server Started" ,but it does not appear . I have noticed that after enabling the socket no code after that works. I have tried searching a lot about this but seem to find no suitable answer.Here is my code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Server
{
public Server(int i1) throws Exception{
ServerSocket MySock=new ServerSocket(i1);//opening server socket
Socket Sock=MySock.accept();//listening to client enabled
JOptionPane.showMessageDialog(null, "Server Started");
}
public static void main(String[] args) {
try {
new Server(2005);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

The problem is that ServerSocket.accept() is blocks until a connection is made..
So the JOptionPane.showMessageDialog(...) will not be executed until someone is connecting to the serversocket.
Here is a solution that handles the ServerSocket in a separate thread
import java.io.IOException;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*;
public class Server
{
public Server(int i1) throws Exception{
Runnable serverTask = () -> {
try {
ServerSocket MySock=new ServerSocket(i1);//opening server socket
while (true) {
Socket Sock=MySock.accept();//listening to client enabled
System.out.println("Accept from " + Sock.getInetAddress());
}
} catch (IOException e) {
System.err.println("Accept failed.");
}
};
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(serverTask);
JOptionPane.showMessageDialog(null, "Server Started");
}
public static void main(String[] args) {
try {
new Server(2005);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Related

Closing Server Socket

I'm trying to close a server but i receive:
java.net.SocketException: Socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
at THREAD.MioServer.AvviaServer(MioServer.java:21)
at THREAD.Avviamento.run(Avviamento.java:16)
at java.lang.Thread.run(Thread.java:745)
This is my code:
package THREAD;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class MioServer {
public int port;
public MioServer(int port) {
super();
this.port = port;
}
ServerSocket serverSocket=null;
Socket socket=null;
public void AvviaServer() throws IOException {
serverSocket=new ServerSocket(port);
System.out.println("serverSocket partito "+ port);
socket=serverSocket.accept();
System.out.println("socket partita");
Scanner in=new Scanner(socket.getInputStream());
PrintWriter out=new PrintWriter(socket.getOutputStream());
while (true) {
String line=in.nextLine();
if (line=="quit") {
break;
}
}
}
public void ferma() throws IOException {
if (socket==null); {
socket=new Socket();
socket.close();
System.out.println("cretino");
serverSocket.close();
}
if (socket!=null) {
socket.close();
socket=null;
serverSocket.close();
System.out.println("chiuso server(?)");
serverSocket=null;
}
}
}
and
package THREAD;
import java.io.IOException;
public class Avviamento implements Runnable{
public MioServer server;
public Avviamento(MioServer server) {
super();
this.server = server;
}
#Override
public void run() {
try {
server.AvviaServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package THREAD;
import java.io.IOException;
public class Fermamento implements Runnable{
public MioServer server;
public Fermamento(MioServer server) {
super();
this.server = server;
}
#Override
public void run() {
try {
server.ferma();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LISTENER:
package LISTENERS;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import THREAD.*;
public class MioListener implements ActionListener {
public int port=3000;
public MioServer server=new MioServer(3000);
#Override
public void actionPerformed(ActionEvent e) {
JButton b=(JButton) e.getSource();
Avviamento avvio=new Avviamento(server);
Thread avviamento=new Thread(avvio);
Fermamento ferma=new Fermamento(server);
Thread fermamento=new Thread(ferma);
if (b.getText().equals("Avvia")) {
System.out.println("Tasto avvia premuto");
avviamento.start();
}
if (b.getText().equals("Stop")) {
System.out.println("Tasto stop premuto");
fermamento.start();
}
}
Thanks for the help.
I'm trying to close a server but i receive:
java.net.SocketException: Socket closed
It's no wonder that your server thread throws up an exception when the Fermamento thread's server.ferma() closes the serverSocket, quasi pulling the rug from under the server's feet.
You could simply catch the exception with something like:
try { socket=serverSocket.accept(); } catch (SocketException e) { return; }

Why socket is being closed in run method?

Server:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class Server {
public static void main(String[] args) {
new Server().start();
}
public void start() {
ExecutorService executorService = Executors.newFixedThreadPool(10);
try (ServerSocket serverSocket = new ServerSocket(1200)) {
while (true) {
try (Socket socket = serverSocket.accept()) {
executorService.submit(new SocketHandler(socket));
} catch (IOException e) {
System.out.println("Error accepting connections");
}
}
} catch (IOException e) {
System.out.println("Error starting server");
}
}
public final class SocketHandler implements Runnable {
private final Socket socket;
public SocketHandler(Socket connection) {
this.socket = connection;
System.out.println("Constructor: is socket closed? " + this.socket.isClosed());
}
#Override
public void run() {
System.out.println("Run method: is socket closed? " + this.socket.isClosed());
}
}
}
Client:
import java.io.IOException;
import java.net.Socket;
public final class Client{
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 1200)) {
} catch (IOException e) {}
}
Output:
Constructor: is socket closed? false
Run method: is socket closed? true
As you can see from output, when run method is invoked socket is closed, but in constructor it was opened.
Question: How to prevent socket being closed in run method, so that I can access its output stream?
Don't use try with resources with a Socket as the resource, since in this situation since the resource, here the socket, will be closed as soon as the try block exits.

Function call on client connection to server

In socket programming using Java.I want a function call to happen whenever a client connects to the server. I'm stuck up here. Any help will be appreciated.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class NewConnectionListener implements Runnable{
public static ServerSocket serverSocket;
public NewConnectionListener(){
try {
serverSocket = new ServerSocket(500);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while(true){
try {
Socket s = serverSocket.accept();
callMethodWithNewSocket(s);
System.out.println("new Client");
} catch (IOException e) {
System.out.println("Error getting Client");
e.printStackTrace();
}
}
}
}
With this code everytime there is a new connection to port 500 on the server the method callMethodWithNewSocket(Socket s) will be called with the socket as a parameter.

Java Networking - Client / Server

My final project for a class is to put together a game, including multiplayer. So I started trying to figure out java networking, and I'm kind of stuck.
Each of the two game clients needs to be able to send and receive messages to and from the other client.
The way I figured I would handle this is that I have a NetworkServer and NetworkClient objects that runs in their own threads.
I was thinking that I would just start them from my main game application, but I wanted to do some testing first, so I set this project up:
NetworkClient:
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
public class NetworkClient extends Thread {
Socket server;
ObjectOutputStream out;
Timer timer;
public NetworkClient(String hostname, int port) throws IOException
{
server = new Socket(hostname, port);
out = new ObjectOutputStream(server.getOutputStream());
timer = new Timer();
timer.schedule(new SendTask(), 0, 1*1000);
}
public void sendData(Integer b) throws IOException {
out.writeObject(b);
}
class SendTask extends TimerTask {
Integer i = new Integer(1);
public void run() {
System.out.println("Client: Sending Integer: " + i.toString());
try {
sendData(i);
i = new Integer(i.intValue()+1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // run()
} // class SendTask
}
NetworkServer:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class NetworkServer extends Thread {
private ServerSocket serverSocket;
private Socket client;
private Integer i;
private ObjectInputStream in;
public NetworkServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
}
public void run()
{
try {
System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
client = serverSocket.accept();
System.out.println("Just connected to " + client.getRemoteSocketAddress());
in = new ObjectInputStream(client.getInputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while(true)
{
try
{
i = (Integer) in.readObject();
System.out.println("Server: Received the integer: " + i.toString());
}
catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
try { client.close();} catch (IOException e1) {}
break;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Network (The thing I'm using to try and test this):
import java.io.IOException;
public class Network {
NetworkClient client;
NetworkServer server;
public Network() throws IOException {
server = new NetworkServer(6066);
server.start();
client = new NetworkClient("192.168.1.196", 6066);
client.start();
}
public static void main(String [] args)
{
try
{
Network n = new Network();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
I have the timer in there to facilitate sending data from the client to the server, which would normally be done by my game, but since I'm testing I had to have it send somehow.
When I run this where the client and server are talking to each other, I get both the Sent and Received messages.
When I put it on my laptop (and change the IP in NetworkClient to match my desktop, and vice versa on my desktop) and run it in both places, the client on the desktop sends to the server on the laptop, but the client on the laptop does not send to the server on the desktop.
And at some point during the running, I get an exception about that client's connection being reset by peer, though the working client/server connection continue.
So, I guess my question is, Does anyone know why it works in one direction but not bidirectionally?
FIXED!!
Edit: Gah, I figured it out. It had to do with the timing on starting the two servers.
I changed Network to:
import java.io.IOException;
public class Network {
NetworkClient client;
NetworkServer server;
public Network() throws IOException {
startServer();
startClient();
}
private void startServer() throws IOException {
server = new NetworkServer(6066);
server.start();
}
private void startClient(){
boolean isConnected = false;
while (!isConnected) {
try {
client = new NetworkClient("192.168.1.196", 6066);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
continue;
}
isConnected = true;
}
client.start();
}
public static void main(String [] args)
{
try
{
Network n = new Network();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Edit your Firewall. Make sure that java.exe has inbound and outbound traffic enabled. Also, add a rule to your Firewall for port 6066.

Thread method doesn't seem to run.(Override the run() method)

im making a server for an application, and I made a thread for accepting user's.
but it seems that it doesn't come to my overrided method run()
it doesn't give me an error or such it just doesn't run.
Here is the code:
This is the Client listener
package org.walking.server.listener;
import java.io.IOException;
import java.net.ServerSocket;
import javax.swing.SwingWorker;
/*
* Walking client listener!
*/
public class WalkingCL {
private SwingWorker work;
ServerSocket server;
public boolean listening = true;
public void acceptclient(){
try {
System.out.println("Created server socket");
server = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Error while creating ServerSocket on port 4444");
e.printStackTrace();
}
work = new SwingWorker<Object,Void>(){
public Object doInBackground(){
while(listening){
try {
new WalkingCLT(server.accept()).start();
} catch (IOException e) {
System.err.println("Error while making thread!");
e.printStackTrace();
}
}
return listening;
}
};
}
}
Here is the client listener thread:
package org.walking.server.listener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/*
* Walking Client listener Thread!
*/
public class WalkingCLT extends Thread {
private Socket client;
public WalkingCLT(Socket client){
super("Walking client listener thread!");
this.client = client;
}
#Override
public void run(){
System.out.println("HELLO?");
try {
System.out.println("User:" + client.getInetAddress() + "connected!");
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out.println("HELLO?");
out.flush();
out.close();
in.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I've put some println statments to see if it comes to that but I only see this:
Server stacktrace:
Created server socket
Client stacktrace:
Panel Created!
Your connected!
Hope you can help me.
Thanks!
You are only creating an instance of the SwingWorker task. You are missing a call to work.execute() or work.doInBackground() by some helper class. You need to look at the ExecutorService and how to use it to submit and execute SwingWorker tasks. There is also a small code snippet in the Future documentation.

Categories