Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying the following simple client-server program on Eclipse but it is not working & giving exceptions.Unable to fix it.Can someone help?
this is one file:
Java
import java.io.*;
import java.net.*;
public class dailyadviceclient 
{
	public void go()
	{
		try
		{
			Socket s=new Socket("127.0.0.1",8080);
			InputStreamReader readr=new InputStreamReader(s.getInputStream());
			BufferedReader br=new BufferedReader(readr);
			String advice=br.readLine();
			System.out.println("Today you should"+advice);
			br.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("Exception"+ex.getMessage());
		}
	}
	public static void main(String[] args) 
	{
		dailyadviceclient cl=new dailyadviceclient();
		cl.go();
	}

}

this is second:
Java
import java.io.*;
import java.net.*;
public class dailyadviceserver 
{
	String[] adviceList = {"Take smaller bites", "Go for the tight jeans. No they do NOT make you look fat.", "One word: inappropriate", "Just for today, be honest. Tell yourboss what you *really* think", "You might want to rethink that haircut."};
	public void go()
	{
		try
		{
			ServerSocket sSock=new ServerSocket(8080);
			while(true)
			{
				Socket sock=sSock.accept();
				PrintWriter wrtr=new PrintWriter(sock.getOutputStream());
				String advice=getAdvice();
				wrtr.println(advice);
				wrtr.close();System.out.println(advice);
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println(ex.getMessage());
		}
	}//go
	public String getAdvice()
	{
		int random=(int)(Math.random()* adviceList.length);
		return adviceList[random];
	}
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		dailyadviceserver sr=new dailyadviceserver();
		sr.go();
	}
}

I dont know if I am making any mistake in executing the program. I am simply doing run as->java application.
Posted
Updated 26-Aug-11 1:58am
v2
Comments
Marc A. Brown 26-Aug-11 8:59am    
You should probably edit your question to include the execption(s) you're getting. You can do that via the "Improve question" link at the bottom of the question.

It's a bit hard to tell since you haven't included the exception message, and also not told if it's the client or server that's failing.

However something like this should work for you (this works for me);

Add two files to your eclipse project (in this example they're called Server and Client but you can call them whatever):

Server.java
Java
package com.bornander.clientservertest;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class Server {
    
    private final static Random random = new Random();
    private final static String[] ADVICE_LIST = {
        "Take smaller bites", 
        "Go for the tight jeans. No they do NOT make you look fat.", 
        "One word: inappropriate", 
        "Just for today, be honest. Tell yourboss what you *really* think", 
        "You might want to rethink that haircut."
    };

    private void go() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        while(!serverSocket.isClosed()) {
            Socket socket = serverSocket.accept();
            
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            String advice = getAdvice();
            System.out.println("Sending advice: " + advice);
            writer.write(advice);
            writer.close();
            System.out.println("Advice sent!");
            socket.close();
        }
    }
    
    private static String getAdvice() {
        return ADVICE_LIST[random.nextInt() % ADVICE_LIST.length];
    }
    
    public static void main(String[] args) throws IOException {
        
        Server server = new Server();
        server.go();
    }
}


Client.java
Java
package com.bornander.clientservertest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Client {

    public void go() throws IOException {
        System.out.println("Getting some good advice...");
        Socket socket = new Socket("localhost", 8080);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String advice = reader.readLine();
        System.out.println(advice);
        reader.close();
        socket.close();
    }
    
    public static void main(String[] args) throws IOException {
        Client client = new Client();
        client.go();
    }
}


Then in Eclipse, right click Server.java and select Debug as Java Application, and then right clock Client.java and select Debug as Java Application, in the Console tab you should see the client getting some good advice.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
[no name] 8-Apr-14 3:40am    
I am new in this field. Can you tell me which server are you using for that? Can you explain me the steps for installing.. and how to use that.

Thank you
Fredrik Bornander 8-Apr-14 4:57am    
I'm not using a server, I'm writing one.
The class Server implements a server by listening for incoming connections and pushing out a message on the socket when a connection is made.
The class Client connects to the server and receives the message.
I am new in this field. Can you tell me which server are you using for that? Can you explain me the steps for installing.. and how to use that.

Thank you
 
Share this answer
 
Comments
TorstenH. 8-Apr-14 3:57am    
The class "Server" you see above is "the server".
Not to be mixed up with the hardware server one might use.

It's onle software, any computer with a server application can be a server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900