I am trying to create a thread to simply send the text to client. However, if you copy this code to IDE, you will see that there is a red underline under client.getOutputStream(). I do not know what is wrong here. The IDE says "Unhandled exception type IOException". Could anybody tell me?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class ServerStudentThread extends Thread {
Socket client;
public ServerStudentThread(Socket x) {
client = x;
}
public void run() {
// create object to send information to client
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
out.println("Student name: ");//send text to client;
}
}
For reference, here is the code that calls the thread.
import java.io.*;
import java.net.*;
import java.util.*;
public class Server2 {
public static void main(String args[]) throws Exception {
int PORT = 5555; // Open port 5555
//open socket to listen
ServerSocket server = new ServerSocket(PORT);
Socket client = null;
while (true) {
System.out.println("Waiting for client...");
// open client socket to accept connection
client = server.accept();
System.out.println(client.getInetAddress()+" contacted ");
System.out.println("Creating thread to serve request");
ServerStudentThread student = new ServerStudentThread(client);
student.start();
}
}
}
It's probably that getOutputStream() can throw an exception and you're not catching it, try putting a try / catch (IOException e) around the block of code.
public void run() {
try {
// create object to send information to client
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
out.println("Student name: ");//send text to client;
} catch (IOException e) {
throw new RuntimeException("It all went horribly wrong!", e);
}
}
So you need to add a try/catch block to handle the I/O exception.
Read the section on Exceptions from the Java tutorial.
From the javadoc:
public OutputStream getOutputStream() throws IOException
IOException is a checked exception. You need to use a try/catch block to handle that possibility.
Kalla,
You need to either put the line in between try/catch block or declare run to throw IOException
Related
I am making an a call to a proxy server and providing the wrong credentials on purpose. However the IOE exception is not being thrown. Instead an internal error with the message that "you should not be here".
Any idea why an IOE is not begin thrown just like the java docs? I created a brand new project just to be sure my other program wasn't causing any issues.
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
public class socketTesting {
public static void main(String[] args) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxyServer>", 8080));
Socket sock = new Socket(proxy);
try
{
sock.connect(new InetSocketAddress("<endpoint server>",443));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
If the proxy creds are correct, the connection works without any issues.
My project aims at reading log messages directly from /dev/log UNIX domain socket in Java. Currently I am using junixsocket. Below is a sample code of client that reads from a unix socket.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import org.newsclub.net.unix.AFUNIXSocketException;
public class SimpleTestClient {
public static void main(String[] args) throws IOException {
final File socketFile = new File("/dev/log");
AFUNIXSocket sock = AFUNIXSocket.newInstance();
try {
sock.connect(new AFUNIXSocketAddress(socketFile));
} catch (AFUNIXSocketException e) {
System.out.println("Cannot connect to server. Have you started it?\n");
System.out.flush();
throw e;
}
System.out.println("Connected");
InputStream is = sock.getInputStream();
byte[] buf = new byte[8192];
int read = is.read(buf);
System.out.println("Server says: " + new String(buf, 0, read));
is.close();
sock.close();
System.out.println("End of communication.");
}
}
The above mentioned code is unable to connect to /dev/log. It throws an exception:
Cannot connect to server. Have you started it?
Exception in thread "main" org.newsclub.net.unix.AFUNIXSocketException: Protocol wrong type for socket (socket: /dev/log)
at org.newsclub.net.unix.NativeUnixSocket.connect(Native Method)
at org.newsclub.net.unix.AFUNIXSocketImpl.connect(AFUNIXSocketImpl.java:125)
at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:97)
at org.newsclub.net.unix.AFUNIXSocket.connect(AFUNIXSocket.java:87)
at SimpleTestClient.main(SimpleTestClient.java:40)
I am unable to figure out how to solve this problem. Any help would be appreciable.
Since you cannot connect to an existing server socket as mentioned in the log traces, then you haven't bound one one the mentioned file, so try creating an AF_UNIX server socket then connect to it.
It could be done in a separate class:
public class DevLogServer {
public static void main(String[] args) throws IOException {
final File socketFile = new File("/dev/log");
AFUNIXServerSocket server = AFUNIXServerSocket.newInstance();
try {
server.bind(new AFUNIXSocketAddress(socketFile));
} catch (Exception e) {
throw e;
}
}
}
Edit as per #Ankit comment:
You may also need to make sure the syslod daemon is stopped by runnig below command in a terminal window:
sudo service syslog stop
You may alternatively need to grand write permission to the /dev directory.
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.
I am on windows 7 x64. I am writing a server which opens a thread for every incoming connection - the thread reads from the connection's input stream. The read() should block and throw an exception if the socket is closed(). It does not - just returns -1. If I do not close the connection from the client - just let the client terminate - I get a connection reset as excpected - but if I close() the connection from the client (or just the client's output stream for that matter) read() in the server thread does not throw - just returns -1. The docs are pretty clear on this :
public void close()
throws IOException
Closes this socket.
Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.
Help
Working code :
Server :
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CloseTest {
private int port;
public CloseTest(int port) {
this.port = port;
}
void base_station_activate() {
ServerSocket baseStationListeningSocket=null;
try {
baseStationListeningSocket = new ServerSocket(this.port, 1, InetAddress.getByName("127.0.0.1"));
} catch (IOException ex) {
}
main_server: while (true) {
try {
Socket clientSocket = baseStationListeningSocket.accept();
BaseStationClientHandler ch = new BaseStationClientHandler(clientSocket);
Thread myThread = new Thread(ch);
myThread.start();
} catch (IOException ex) {
System.exit(1);
} // main_server
finally {
// baseStationListeningSocket.close()
}
}
}
public static void main(String args[]){
CloseTest bs = new CloseTest(8082);
bs.base_station_activate();
}
public class BaseStationClientHandler implements Runnable {
private final Socket clientSocket;
private BaseStationClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
String debug_message = null;
try {
InputStream in = clientSocket.getInputStream();
// read message and respond
String s = "";
char x;
int r;
server: while (true) {
try {
while ((r = in.read()) != (int) '%') {
if (r == -1) {
debug_message = "Stream/socket .closed() - exception not thrown (WHYYYYY ????) by client";
System.out.println(debug_message);
break server;
}
x = (char) r;
s += x;
}
System.out.println(s);
} catch (SocketException socketException) {
System.out.println(socketException.getLocalizedMessage()); // if connection reset (but not if Stream/socket .closed()) read throws !!!!!
debug_message = "socket_reset";
break server;
}
s = "";
} //server
} catch (IOException ex) {
System.out.println("IOexception in client handler - check if thrown by read");
} finally {
try {
clientSocket.close();
} catch (IOException ex) {
}
}
}
}
}
Client :
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;
public class CloseTestClient {
public CloseTestClient(int port, String ipAddress){
Vector<Socket> connections = new Vector<Socket>();
try {
for(int i=0;i<20;i++){
Socket connection = new Socket(InetAddress.getByName(ipAddress), port);
connections.add(connection);
OutputStream out = connection.getOutputStream();
out.write( ("CONNECT#"+(i+1)+"#1%").getBytes());
System.out.println("[CloseTestClient SENT]:"+"CONNECT#"+(i+1)+"#1%");
Thread.sleep(1000); // to be sure the server threads are blocked in the read()
// connection.close(); // if I comment this out I see the connection reset message from the server when this main terminates
// commented it out finally and moved the closing at the end to be sure the server threads are blocked in read()
}
} catch (Exception ex) {
ex.printStackTrace();
}
finally{
// if I comment *for* out I see the "connection_reset" message from the server when this main terminates
for (Socket c : connections){
try{
c.close();
}catch(Exception ex){
}
}
}
}
public static void main(String args[]){
System.out.println("CloseTestClient run !");
new CloseTestClient(8082,"127.0.0.1");
}
}
The bit of documentation you're referring to applies to threads on that machine, not remote threads. If you have thread A read()'ing on socket X, and thread B of the same process closes socket X, then an exception will be thrown for thread A's read call.
When a socket is close()'d on the the local machine, the remote machine can determine that there will be no more data coming over the socket so it returns -1 (see the read() documentation for InputStream). This is what is happening when you explicitly close the connection on the client. The server knows no more data will be coming so read() happily returns -1. There are no exceptional circumstances.
I'm guessing that when you let the client terminate without calling close() on the socket, the JVM or OS is sending a TCP RST instead of closing the connection nicely (sending TCP FIN). This causes the read() call on the server to throw an exception.
I'm building a middleware, where I need to constantly read what is happening in my device, so I build this class:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* #author Valter
*/
public class Middleware {
public static void main(String args[]) {
try {
// ip and port where is my device
Socket socket = new Socket("192.168.1.4", 2001);
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
// i need send this parameter to my device
dataOutputStream.writeUTF("}Rv!");
String answer = dataInputStream.readUTF();
System.out.println("Answer:"+answer);
dataInputStream.close();
dataOutputStream.close();
socket.close();
} catch (UnknownHostException ex) {
System.out.println("UNKNOW HOST EXCEPTION");
} catch (IOException ex) {
System.out.println(" IOEXCEPTION");
System.out.println(ex.getMessage());
}
}
}
Output:
IOEXCEPTION Connection reset
What's wrong with my class?
I don't know if this is what is causing your problem, but you are not flushing the output stream before attempting to read a response from the input stream. Try:
dataOutputStream.writeUTF("}Rv!");
dataOutputStream.flush();
Does your device really understand the output of writeUTF()?and produce the correct input for readUTF()? Check the Javadoc. I don't think it likely.
'Connection reset' usually means you have written to a connection that was already closed by the other end, which is already an application protocol error, and it may indicate prior application protocol errors.