【发布时间】:2018-04-23 04:43:23
【问题描述】:
我想知道如何将消息从服务器发送回客户端。我是 Java 新手,并且已经搜索了问题,但是使用的代码对我一直在学习的内容不熟悉。我已经尝试过这样做,但是我不能完全让它将消息发送回客户端。
一旦客户端将消息“首先”发送到服务器,我想从服务器发送消息“revieved”消息。
任何关于解释的帮助将不胜感激!
客户代码:
import java.io.*;
import java.net.*;
public class Client {
//Main Method:- called when running the class file.
public static void main(String[] args){
//Portnumber:- number of the port we wish to connect on.
int portNumber = 15882;
//ServerIP:- IP address of the server.
String serverIP = "localhost";
try{
//Create a new socket for communication
Socket soc = new Socket(serverIP,portNumber);
// create new instance of the client writer thread, intialise it and
start it running
ClientWriter clientWrite = new ClientWriter(soc);
Thread clientWriteThread = new Thread(clientWrite);
clientWriteThread.start();
}
catch (Exception except){
//Exception thrown (except) when something went wrong, pushing
message to the console
System.out.println("Error --> " + except.getMessage());
}
}
}
//This thread is responcible for writing messages
class ClientWriter implements Runnable
{
Socket cwSocket = null;
public ClientWriter (Socket outputSoc){
cwSocket = outputSoc;
}
public void run(){
try{
//Create the outputstream to send data through
DataOutputStream dataOut = new
DataOutputStream(cwSocket.getOutputStream());
System.out.println("Client writer running");
//Write message to output stream and send through socket
dataOut.writeUTF("First"); // writes to output stream
dataOut.flush(); // sends through socket
//close the stream once we are done with it
dataOut.close();
}
catch (Exception except){
//Exception thrown (except) when something went wrong, pushing
message to the console
System.out.println("Error in Writer--> " + except.getMessage());
}
}
}
class ClientListener implements Runnable
{
Socket clSocket = null;
public ClientListener (Socket inputSoc) {
clSocket = inputSoc;
}
public void run() {
try {
// need to write here to recieve message
DataInputStream dataIn = new
DataInputStream(clSocket.getInputStream()); // new stuff
String msg = dataIn.readUTF();
System.out.print(msg);
}
catch (Exception except){
//Exception thrown (except) when something went wrong, pushing
message to the console
System.out.println("Error in Writer--> " + except.getMessage());
}
}
}
服务器代码:
import java.io.*;
import java.net.*;
public class Serv {
//Main Method:- called when running the class file.
public static void main(String[] args){
//Portnumber:- number of the port we wish to connect on.
int portNumber = 15882;
try{
//Setup the socket for communication
@SuppressWarnings("resource")
ServerSocket serverSoc = new ServerSocket(portNumber);
while (true){
//accept incoming communication
System.out.println("Waiting for client");
Socket soc = serverSoc.accept();
DataOutputStream dos = new
DataOutputStream(soc.getOutputStream());
dos.writeUTF("Message Recieved");
// new stuff
dos.flush(); //need to flush
//create a new thread for the connection and start it.
ServerConnetionHandler sch = new ServerConnetionHandler(soc);
Thread schThread = new Thread(sch);
schThread.start();
}
}
catch (Exception except){
//Exception thrown (except) when something went wrong, pushing
message to the console
System.out.println("Error --> " + except.getMessage());
}
}
}
class ServerConnetionHandler implements Runnable
{
Socket clientSocket = null;
public ServerConnetionHandler (Socket inSoc){
clientSocket = inSoc;
}
public void run(){
try{
//Catch the incoming data in a data stream, read a line and output
it to the console
DataInputStream dataIn = new
DataInputStream(clientSocket.getInputStream());
System.out.println("Client Connected");
//Print out message
System.out.println("--> " + dataIn.readUTF());
//close the stream once we are done with it
dataIn.close();
}
catch (Exception except){
//Exception thrown (except) when something went wrong, pushing
message to the console
System.out.println("Error in ServerHandler--> " +
except.getMessage());
}
}
}
【问题讨论】:
标签: java sockets networking server client