In my main thread I have a while(listening) loop which calls accept() on my ServerSocket object, then starts a new client thread and adds it to a Collection when a new client is accepted.
However, the accept() call in the while(listening) loop blocks and i'm struggling to close the connection. please find my below sample code, is there any way to create wrapper service to close the socket connection for the below server side program.
public static final void serverSocket(IData pipeline) throws ServiceException {
int clientNumber = 0;
ServerSocket listener = null;
int port =0;
Properties socketProperties = new Properties();
try
{
listener = new ServerSocket(port);
System.out.println(listener.getLocalSocketAddress());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(true){
new Connections(listener.accept(), clientNumber++).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
finally{
try {
listener.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static class Connections extends Thread {
private Socket socket;
private int clientNumber;
public Connections(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
public void run()
{
try {
// Send a welcome message to the client.
while (true)
{
// my program
}
catch (IOException e) {
//exception
}
finally {
try {
socket.close();
}
catch (IOException e) {
log.error("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
Any suggestions would be much appreciated.
You have not adequately described the circumstances that dictate when you want to close.
In any case, your serverSocket object could become a static field that is made available to some other thread(s), and that other thread can decide when to close.
Related
I've been having some trouble with a project that requires a bit of networking, where data is sent over a SocketChannel but is never received. I was able to replicate the issue with a simple localhost chatroom program (sorry if it's a bit messy):
public class Main {
private Sender sender;
private Receiver receiver;
public static void main(String[] args) {
Main foo = new Main();
//The ports are switched in the other running version of this
foo.receiver = new Receiver("192.168.1.108", 12348);
foo.sender = new Sender("192.168.1.108", 12347);
foo.takeUserInput();
}
private void takeUserInput() {
while(true) {
System.out.println("Enter something");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sender.send(input);
}
}
}
public class Receiver implements Closeable {
private InetSocketAddress bindAddress;
private ServerSocketChannel server;
private ListenThread listenThread;
public Receiver(String address, int port) {
bindAddress = new InetSocketAddress(address, port);
bind();
listen();
}
public void bind() {
try {
server = ServerSocketChannel.open();
server.configureBlocking(true);
server.bind(bindAddress);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Bound to port " + bindAddress.getPort());
}
public void listen() {
listenThread = new ListenThread();
listenThread.start();
}
private class ListenThread extends Thread {
private SocketChannel client;
public void run() {
try {
client = server.accept();
System.out.println("Received connection from " + client.getLocalAddress());
} catch (IOException e) {
e.printStackTrace();
}
while((server.isOpen()) && (client.isOpen())) {
byte[] bytes = new byte[4096];
ByteBuffer buffer = ByteBuffer.wrap(bytes);
try {
System.out.println("Reading");
client.read(buffer);
System.out.println(new String(buffer.array()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
#Override
public void close() throws IOException {
server.close();
listenThread.client.close();
}
}
public class Sender implements Closeable {
private InetSocketAddress connectAddress;
private SocketChannel clientChannel;
public Sender(String address, int port) {
connectAddress = new InetSocketAddress(address, port);
connect();
}
public void connect() {
while((clientChannel == null) || (!(clientChannel.isConnected()))) {
try {
clientChannel = SocketChannel.open(connectAddress);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
System.out.println("Connected to " + clientChannel.getLocalAddress());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void send(String message) {
byte[] bytes = message.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
try {
clientChannel.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Sent message");
}
#Override
public void close() throws IOException {
clientChannel.close();
}
}
Here's the logs from one version:
Bound to port 12348
Reading
Connected to /192.168.1.108:64699
Enter something
thing
Sent message
Enter something
And the other:
Bound to port 12347
Reading
Connected to /192.168.1.108:64698
Enter something
So, I know that both programs successfully establish connections to the other, and start reading, but when I send something over a SocketChannel from one end, the other just remains stuck on the read()call in ListenThread.
How can I make the client successfully read what is sent?
I can see two issues.
As mentioned by #Ethan F, the ports are different. You should use the same port number.
Your listen() method in the Receiver class never get called. You need to call this method to accept connection.
foo.receiver = new Receiver("192.168.1.108", 12348);
foo.receiver.listen();
I have simple client and server, the client is based on NIO where as the server is a simple old style program.
I am using the client in its default mode which is blocking. In the program i try to write from the client side, server reads it. Then server replies and the client reads it.
I am able to write into the server with no issues, but the read from the Server in the client is proving to be problematic. As it is in blocking mode, i expect that it never returns 0 according to the documentation. But thats not what is happening, i always see the return from client_channel.read as 0.
*******************************SERVER*******************************************
class MyBlockingServer extends Thread
{
private int M_PortNumber;
private ServerSocket M_ServerSocket;
MyBlockingServer(int PortNumber)
{
M_PortNumber = PortNumber;
try {
M_ServerSocket = new ServerSocket(M_PortNumber);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run()
{
int my_number = 0;
while(true)
{
try {
Socket ClientServerTuple = M_ServerSocket.accept();
//System.out.println("Server address is "+ ClientServerTuple.getLocalAddress() + "Server Port is " + ClientServerTuple.getLocalPort());
//System.out.println("Client address is " + ClientServerTuple.getRemoteSocketAddress() + "Client address is" + ClientServerTuple.getPort());
DataInputStream inputStream = new DataInputStream(ClientServerTuple.getInputStream());
byte b[] = new byte[48];
inputStream.read(b);
System.out.println("[SERVER]" + new String(b));
DataOutputStream outputStream = new DataOutputStream(ClientServerTuple.getOutputStream());
byte c[] = new byte[100];
String output= new String("Thanks for connection, you suck tata" + " "+ my_number);
c = output.getBytes();
outputStream.write(c);
my_number++;
System.out.println("write done");
ClientServerTuple.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void socket_close()
{
try {
M_ServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class JavaBlocking
{
public static void main(String []args)
{
MyBlockingServer Server = new MyBlockingServer(8000);
try {
Server.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*******************************CLIENT*******************************************
public class JavaChannels
{
public static void main(String []args)
{
SocketChannel client_channel = null;
try {
client_channel = SocketChannel.open();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel open");
try {
client_channel.connect(new InetSocketAddress("127.0.0.1",8000));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel connected");
ByteBuffer my_buffer = ByteBuffer.allocate(248);
try {
my_buffer.put("seven77".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
my_buffer.flip();
try {
int bytes_written = client_channel.write(my_buffer);
while(my_buffer.hasRemaining())
{
bytes_written = client_channel.write(my_buffer);
}
System.out.println("[Async Client] Wrote "+ bytes_written +" bytes");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("[Async Client] Socket channel write finished");
my_buffer.clear();
my_buffer.flip();
try {
int read_length = client_channel.read(my_buffer);
System.out.println("Initial read is " + read_length + " bytes");
while(read_length !=-1)
{
read_length = client_channel.read(my_buffer);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading the buffer." +"Read "+read_length +"bytes");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("[Async Client] server says" + new String(my_buffer.array()));
try {
client_channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The output i am seeing from the client side is as follows
Initial read is 0 bytes
Reading the buffer.Read 0bytes
Reading the buffer.Read 0bytes
Reading the buffer.Read 0bytes
I think that this is wrong:
System.out.println("[Async Client] Socket channel write finished");
my_buffer.clear();
my_buffer.flip();
The clear prepares the buffer for reading by setting the position to zero and the limit to the capacity.
But the flip then sets the limit to the position; i.e. zero. That means than when you attempt to read into the buffer, there is space for zero bytes.
Get rid of that flip call.
As it is in blocking mode, i expect that it never returns 0 according to the documentation.
Which documentation? The javadocs for SocketChannel.read(ByteBuffer) says:
"It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read. "
In this case, the highlighted condition is false.
I have a Java Server and one(or more) Android Clients. For now I want them to communicate simply with strings. When i write from android I can get the data in Java Server, but when I try to get the answer from server the Android application stop working. The codes is reported below:
Java Server:
public class Server {
private static int port=12346, maxConnections=0;
// Listen for incoming connections and handle them
public static void main(String[] args) {
int i=0;
try{
ServerSocket listener = new ServerSocket(port);
Socket server;
while((i++ < maxConnections) || (maxConnections == 0)){
doComms connection;
server = listener.accept();
String end = server.getInetAddress().toString();
System.out.println("\n"+end+"\n");
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
t.start();
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
class doComms implements Runnable {
private Socket server;
private String line,input;
public doComms(Socket server) {
this.server=server;
}
#SuppressWarnings("deprecation")
public void run () {
input="";
try {
// Get input from the client
DataInputStream in = new DataInputStream (server.getInputStream());
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(server.getOutputStream())),
true);
while((line = in.readLine()) != null && !line.equals(".")) {
input=input + line;
}
JOptionPane.showMessageDialog(null, input);
out.println("Enviado");
server.close();
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
And Android client's code (it's called every time a button is pressed inside onClick method):
public String enviaMensagem(){
String resposta="";
new Thread(new ClientThread()).start();
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket(ip, port);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(input.getText().toString());
resposta = dataInputStream.readUTF();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return resposta;
}
You are using an unsorted mixture of readUTF(), writeUTF(), readLine(), etc. They're not all interoperable. Settle on one of them. If you use writeUTF() you must use readUTF() at the other end. If you use readLine() you must write lines at the other end, with a line terminator such as \r\n or \n.
i've done a socket programming with client residing on android and server residing on the desktop ....... whenever server is down as we know client lost the connection.....so it undergoes a looping until it connects to server.......
here the problem is in the below code
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
in the while loop in the case of lost connection.........but when the connection is on it again creates a new object........
can anyone please tell me how to solve this problem..........
In the client side
while(true){
try {
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
tabletout = new PrintWriter(tabletclient.getOutputStream());
in = new Scanner(tabletclient.getInputStream());
try
{
if((line = in.nextLine())!=null)
{
// my task to be done
}
}catch(Exception d){
System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"+tabletclient.isConnected());
}
} catch (UnknownHostException e) { System.out.println("Entered 2.........");
} catch (IOException e) { System.out.println("Entered 3.........");e.printStackTrace();
}
}
In in the Server side
:
:
private Set <Socket> TABhs=new HashSet<Socket>();
:
:
new Thread(new TABServerThread()).start(); // runs in background
:
:
:
class ServerThread implements Runnable {
private ServerSocket server;
#Override
public void run() {
try {
server = new ServerSocket(SERVER_PORT);
System.out.println("Server Start the server at port " + SERVER_PORT
+ " and waiting for clients...");
while (true) {
Socket socket = server.accept();
System.out.println("Server Accept socket connection: "
+ socket.getLocalAddress());
new Thread(new ClientHandler(socket)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static PrintWriter out;
class ClientHandler implements Runnable {
private Socket clientSocket;
private Scanner in;
public ClientHandler(Socket clietSocket) {
this.clientSocket = clietSocket;
}
#Override
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream());
in = new Scanner(clientSocket.getInputStream());
String line;
System.out.println("ClientHandlerThread Start communication with : "+ clientSocket.getLocalAddress());
try{
while((line = in.nextLine()) != null) {
System.out.println("ClientHandlerThread Client says: " + line);
String dat[]=line.split("#");
String query="insert into table_orders (tableno,kotno, orders,status) values('"+dat[1]+"','"+dat[0]+"','"+dat[2]+"','pending')";
try {
int i= dbGetDet.insertDetails(query);
if(i>0)
{
fillTable();
filtercomboBox();
out.print("success");
out.flush();
for(Socket so:TABhs)
{
PrintWriter ot = new PrintWriter(so.getOutputStream());
ot.println("tableallocation#"+dat[1]);
ot.flush();
}
System.out.println("SENDED 'SUCCESS' TO CLIENT");
}
} catch (Exception ex) {
Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
}
// }
}
}catch(Exception r){}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the Button click of server
String stat=status_combo.getSelectedItem().toString();
String tables=tableno_combo.getSelectedItem().toString();
String kotno=kotno_combo.getSelectedItem().toString();
if(stat.equals("Processing"))
{
try {
TABhs = new CopyOnWriteArraySet(TABhs);
int soint=1;
System.out.println("TABhs Processing--------------------->"+TABhs.size());
for(Iterator <Socket> it=TABhs.iterator();it.hasNext();)
{
Socket so=it.next();
System.out.println("SEEE SOCKET Processing"+soint+"----->"+so.isClosed());
PrintWriter ot = new PrintWriter(so.getOutputStream());
ot.println("tableupdate#"+tables+"#"+kotno+"#processing");
ot.flush();
JOptionPane.showMessageDialog(rootPane, "<html><body>Table Kot Status Changed to <b>Processing</b></body></html>");
soint++;
}
System.out.println("TABhs Processing--------------------->"+TABhs.size());
}
catch (Exception ex) {
Logger.getLogger(MYClientclass.class.getName()).log(Level.SEVERE, null, ex);
}
}
NOW EACH TIME WHEN BUTTON IS CLICKED THE OUTPUT IS AS GIVEN BELOW
FISRT CLICK
SEEE SOCKET Ready 1----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 2----->false
TABhs--------------------->2
SECOND CLICK
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 3----->false
TABhs--------------------->4
FOURTH CLICK
SEEE SOCKET Ready 1----->false
SEEE SOCKET Ready 2----->false
SEEE SOCKET Ready 3----->false
Server Accept socket connection: /192.168.1.74
SEEE SOCKET Ready 4----->false
TABhs--------------------->5
I think the problem is at the client's side you read a line and then create a new connection.
I think you must keep reading the socket until it's closed or an error occurs.
For example:
while (true)
{
tabletclient = null;
int loop = 0;
// loop until a connection is established
while (tabletclient == null)
{
try
{
tabletclient = new Socket(SERVER_IP, TAB_SERVER_PORT);
}
catch (Exception e)
{
e.printStackTrace();
// set the value to quit when no connection could be established
if (loop++ > 100)
return;
}
}
try
{
tabletout = new PrintWriter(tabletclient.getOutputStream());
in = new Scanner(tabletclient.getInputStream());
// read the socket until it's closed or an error occurs
try
{
while ((line = in.nextLine()) != null)
{
// my task to be done
}
}
catch (Exception d)
{
System.out.println("Connection from server has lost.........tabletclient.isConnected()----->"
+ tabletclient.isConnected());
}
tabletsocket.close();
}
catch (UnknownHostException e)
{
System.out.println("Entered 2.........");
}
catch (IOException e)
{
System.out.println("Entered 3.........");
e.printStackTrace();
}
}
Also, you must close the server side when the transfer from the server to the client is completed.
I have the following classes
ClientDemo.java
ClientTread.java
ServerDemo.java
ServerThread.java
public class ClientDemo {
public static void main(String[] args) throws InterruptedException {
try {
Socket client=new Socket("localhost", 6666);
while(true)
{
System.out.println("Hello");
Thread th=new Thread(new ClientThread(client));
th.start();
System.out.println("Thread started........");
th.sleep(1000*30);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ClientThread.java
public class ClientThread implements Runnable {
Socket c;
public ClientThread(Socket client) {
this.c=client;
}
#Override
public void run() {
DataOutputStream dos=null;
try {
System.out.println("Client thread is going to write.......");
dos = new DataOutputStream(c.getOutputStream());
dos.writeUTF("Hello From Client");
System.out.println("Data written by client............");
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println(e+"");
}
}
}
SeverDemo.java
public class ServerDemo {
public static void main(String[] args) {
try {
ServerSocket serversocket=new ServerSocket(6666);
System.out.println("server listening..........");
Thread ts=new Thread( new ServerThread(serversocket.accept()));
ts.start();
System.out.println("server thread started.........");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ServerThread.java
public class ServerThread implements Runnable {
Socket s;
public ServerThread(Socket server) {
this.s=server;
}
#Override
public void run() {
DataInputStream dis;
try {
dis = new DataInputStream(s.getInputStream());
String message=dis.readUTF();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
the code executes perfectly once,after that i get the following error
In Client Console
Hello
Thread started........
Client thread is going to write.......
Data written by client............
Hello
Thread started........
Client thread is going to write.......
java.net.SocketException: Connection reset by peer: socket write error
In your ServerThread,
public void run() {
DataInputStream dis;
try {
dis = new DataInputStream(s.getInputStream());
String message=dis.readUTF();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The execution halts after a single line is read from client. Hence server code exits.Thus you are getting the exception.
So what you can do is:
public void run() {
DataInputStream dis;
try {
while(true)
{
dis = new DataInputStream(s.getInputStream());
String message=dis.readUTF();
System.out.println(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here, we have enclosed the code inside the run method in a while loop which keeps the server running for ever. You can put your own logic.
Hope that helps!!
The server starts a thread on the socket which reads a message from the socket input stream, then writes it to the consoler, then stops running. The socket and its stream thus go out of scope and are probably garbage-collected, causing the socket to be closed. And since the socket is closed, the client can't write anything anymore.
If you expect the client to be able to send an infinity of messages, then the server should loop and read an infinity of messages.