I recently learn about Socket Programming between client and server. So I thought of doing an exercise of connecting both client and server. However, I have encountered this error message when I try to run the code: Exception in thread "main" java.net.ConnectException: Connection refused: connect
This is my client class code:
public class clientpart {
public static void main(String[]args) throws UnknownHostException, IOException {
Scanner input = new Scanner(System.in);
int port = 8080;
String host=null;
String answer; String sendMessage; String receivedMessage;
InetAddress address = InetAddress.getByName(host);
Socket socket= new Socket(address,port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
System.out.println("Please answered the following question: ");
System.out.println("What is the subject code for Socket Programming?");
answer = input.nextLine();
sendMessage = answer;
bw.write(sendMessage);
bw.newLine();
bw.flush();
System.out.println("Message sent to server: "+sendMessage);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
receivedMessage = br.readLine();
System.out.println("Message received from server : " + receivedMessage);
}
}
This is my server code:
public class serverpart {
public static Socket socket;
public static void main(String[]args) throws IOException {
int port = 8080;
String answer; String returnedMessage; String reply;
ServerSocket server = new ServerSocket(port);
System.out.println("Server start at port "+port+".");
while(true)
{
socket = server.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
answer = br.readLine();
System.out.println("Message sent from client: " + answer);
if("NET 3202".equals(answer) || "net 3202".equals(answer) || "NET3202".equals(answer) || "net3202".equals(answer)){
reply = "Correct!";
returnedMessage = reply;
}
else{
reply = "Wrong!";
returnedMessage = reply;
}
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnedMessage);
bw.newLine();
System.out.println("Message replied to client: "+returnedMessage);
bw.flush();
}
}
}
The full error message is:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at clientpart.main(clientpart.java:13)
C:\Users\PeiErn\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
I hope someone can help me, thanks.