Java客户端 - 服务器程序

问题描述:

我正在Eclipse上尝试以下简单的客户端 - 服务器程序,但它无法运行&给出例外。无法修复它。可以帮助人吗?

这是一个文件:

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:

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:

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();
	}
}



我不知道我在执行程序时是否犯了错误。我只是在运行as-> java应用程序。


I dont know if I am making any mistake in executing the program. I am simply doing run as->java application.

由于你没有包含异常消息,所以有点难以辨别,也没有告诉我这是客户端或服务器失败。



但是这样的事情对你有用(这对我有用);



将两个文件添加到您的eclipse项目中(在此示例中,它们被称为服务器客户端,但您可以将其称为任何内容):



Server.java

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
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



Client.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();
    }
}





然后在 Eclipse 中,右键单击服务器.java 并选择 Debug as Java Application ,然后右键 Client.java 并选择 Debug as Java Application ,在控制台标签,你应该看到客户得到一些好建议。



希望这会有所帮助,

Fredrik



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


我是这个领域的新手。你能告诉我你用的是哪台服务器吗?你能解释我安装的步骤..以及如何使用它。



谢谢
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