I am getting this error when I'm running my client program. I was unable to recognise the problem yet.I've changed the port numbers but there is no use. I saw the previous posts regarding the same error but I didn't figured it out.
Server.java
import java.io.*;
import java.net.*;
class Server{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(8080);
while (true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new
DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Client.java
import java.io.*;
import java.net.*;
class Client
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 8080);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
I am getting an error when I run the client.java
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:11)
Can anyone help with this error?
The Client and Server Program runs perfectly fine for me.
Can you check if there is some process already listening on that port.
Related
This is just a very Simple Server/Client application that i have written in Java with the intention using BufferedReader and BufferedWriter only.
I dont get any Errors when i start the Server.
But when the Client tries to send a message to the server:
java.net.SocketException: Connection reset
appears. I dont know why this happens but i think it could have to do something with the BufferedWriter maybe.
I searched in the Internet but i haven't found anything usefull specific to this.
Server.java
import java.io.*;
import java.net.*;
public class Server {
public Server(int port) {
try {
ServerSocket seso = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Server listening on port " + port);
Socket client1 = seso.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client1.getInputStream(), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client1.getOutputStream(), "UTF-8"));
String s = null;
while((s = reader.readLine()) != null) {
System.out.println("Client: " + s);
}
writer.close();
reader.close();
} catch (Exception e) {e.printStackTrace();}
}
public static void main(String args[]) {
new Server(4444);
}
}
Client.java
import java.io.*;
import java.net.*;
public class Client {
public Client(String serverip) {
try {
Socket client = new Socket(serverip, 4444);
System.out.println("Client connected with: " + serverip);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "UTF-8"));
writer.write("Hello Server");
writer.flush();
}catch(Exception e){e.printStackTrace();}
}
public static void main(String args[]) {
new Client("localhost");
}
}
And when i run the server followed by the client i get this from the console:
Server started
Server listening on port 4444
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 9 seconds)
If i add:
writer.newLine();
after
writer.write("Hello Server");
in Client.java i still get:
Server started
Server listening on port 4444
Client: Hello Server
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Server.<init>(Server.java:24)
at Server.main(Server.java:33)
BUILD SUCCESSFUL (total time: 2 seconds)
I don't know what's wrong with the code as it always resets the connection when I send an input from Client to Server. The error report indicates problem on get.readLine() on Client code.
This is the code on Client Side:
import java.net.*;
import java.util.ArrayList;
import java.io.*;
public class Client
{
public static void main(String[] args) throws UnknownHostException, IOException
{
String hostName = "localhost";
Socket client = null;
try{
client = new Socket(hostName, 9972);
}catch(UnknownHostException e) {
System.err.println("Host unknown. Cannot establish connection");
} catch (IOException e) {
System.err.println("Cannot establish connection. Server may not be up." + e.getMessage());
}
PrintWriter send = new PrintWriter(client.getOutputStream(), true);
BufferedReader get = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a 7-bits binary to send: ");
String input;
while((input = read.readLine()) != null)
{
String codeWord = addParity(input);
send.println(codeWord);
System.out.println("Server responds: " + get.readLine());
}
send.close();
get.close();
read.close();
client.close();
}
public static String addParity(String data)
{
//Some code block here
}
}
This is the code on Server Side:
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.ArrayList;
public class Server
{
public static void main(String[] args) throws IOException
{
int port = 9972;
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
PrintWriter send = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader get = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String input;
while((input = get.readLine()) != null)
{
if(input.length() == 11 && input.matches("[01]+"))
{
String data = "";
Random rand = new Random();
int change = rand.nextInt(1);
for(int I = 0; I < change; i++)
{
data = modify(input);
}
String result = HammingCheck(data);
send.println(result);
}
else
send.println("Input is invalid. Echo: " + input);
if(input.equals("bye"))
break;
}
send.close();
get.close();
clientSocket.close();
serverSocket.close();
}
public static String modify(String input)
{
//Some code to modify the string
}
public static String HammingCheck(String data)
{
//Some code to perform hamming code checking
}
public static String removeParity(String data)
{
//some code to remove parity bits
}
}
This is the error report:
Attempting to connect to server at host: localhost
Connection established.
Enter a 7-bits binary string as message to server: 1111111
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at ClientSide.Client.main(Client.java:35)
I'm nowhere near done with this project yet I'm just trying to make sure things are working and more specifically that the client and server are connecting, and I'm getting connection errors. Here are my two programs. I'm also not entirely sure how to run both of them at once in eclipse so maybe that is the problem. Any help is much appreciated
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
public void main(String[] args)throws Exception {
ServerSocket server = new ServerSocket(3333);;
while (true)
{
Socket clientsocket = server.accept();
Scanner sc = new Scanner(clientsocket.getInputStream());
PrintStream p = new PrintStream(clientsocket.getOutputStream());
String message = "Hello from server";
p.print(message);
}
}
And heres the client
public static void main(String[] args)throws Exception {
Client display = new Client();
display.setVisible(true);
try {
Socket server = new Socket("localhost", 3333);
Scanner sc = new Scanner(server.getInputStream());
PrintStream p = new PrintStream(server.getOutputStream());
System.out.println(sc.next());
server.close();
sc.close();
p.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have some gui stuff above and below the client that I didnt show. When I run these I get the following errors
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:56)
and line 56 is the line with -- Socket server = new Socket("localhost", 3333);
If i start the Server first and then the Client everything works perfect, but when i start the client first and then the server(making sure that the Client is able to connect when the svr crashes and then goes online again) the Client does connect to the Server, but after 2,3 seconds it throws a SocketException: Connection reset. I don't know what is causing it and i would really apprechiate it if someone could help me figure this out.
Server code that handles the clients:
public ServerHandler(Socket socket){
try{
pw = new PrintWriter(socket.getOutputStream());
writerHolder[userCounter] = pw;
InputStreamReader in = new InputStreamReader(socket.getInputStream());
reader = new BufferedReader(in);
userCounter++;// Increment the number of people connected
}catch(Exception ex)
{
ex.printStackTrace();
}
}
Client code that connects to the Server:
private void startConnection()
{
try
{
sock = new Socket("192.168.1.5", 5000);
InputStreamReader input = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(input);
pw = new PrintWriter(sock.getOutputStream());
JOptionPane.showMessageDialog(null, "Connected to the server!");
}catch(IOException ex)
{
//timer.reconnectTimer(20);
ex.printStackTrace();
}
}
StackTrace:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at homeControl.ServerHandler.run(ServerHandler.java:52)
at java.lang.Thread.run(Unknown Source)
"Connection reset" usually means that the remote side closed its side of the socket without reading the data that you sent. Make sure that you read data sent to you before you close a socket.
I am trying to use ServerSocket with port 2649, and other people cannot connect. It works fine with localhost. This is the error people get when trying to connect:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.main(Client.java:11)
I have port forwarded, and I do not have a firewall active on my computer. Here are the settings I used when port forwarding.
http://i.imgur.com/NLdaA.png
http://i.imgur.com/FJpJQ.png
When I check port 2649 on canyouseeme.org, it says the connection timed out.
I am using Windows XP too. Any help is appreciated.
Thanks
EDIT: Here is the code I am using
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args)throws Exception {
System.out.println("Starting...");
File file = new File("C:/Testing.txt");
InputStream in = new FileInputStream(file);
ServerSocket server = new ServerSocket(2649);
System.out.println("Ready for connection");
Socket socket = server.accept();
OutputStream output = socket.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(output);
out.writeObject("C:/Testing.txt");
byte[] buffer = new byte[socket.getSendBufferSize()];
int bytesReceived = 0;
while ((bytesReceived = in.read(buffer)) > 0) {
output.write(buffer, 0, bytesReceived);
}
out.flush();
out.close();
in.close();
server.close();
socket.close();
output.flush();
output.close();
System.out.println("Finished");
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
System.out.println("Starting...");
Socket socket = new Socket("IP ADDRESS", 2649);
InputStream input = socket.getInputStream();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
FileOutputStream out = new FileOutputStream(new File((String) in.readObject()));
byte[] buffer = new byte[socket.getReceiveBufferSize()];
int bytesReceived = 0;
while ((bytesReceived = input.read(buffer)) > 0) {
out.write(buffer, 0, bytesReceived);
}
in.close();
out.close();
input.close();
socket.close();
System.out.println("Finished");
}
if it's not the firewall. make sure you bind the server socket to 0.0.0.0 and not to localhost.
try calling server.bind(new InetSocketAddress("0.0.0.0", port));
"Connection timed out" -> a firewall discards packets. Most likely Windows Firewall - try disabling it and see if they can connect.