Servlet中写Socket,载本地运行测试,没有出现想要的效果。全国人民惊呆了 5C

服务端


import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;


/**
 * 功能简介:我在Servlet里写个Socket,这样服务器跑起来后,客户端就能和服务器对话了。
 * @author nazi
 */
@WebServlet(name = "MyServlet", urlPatterns = {"/my"})
public class MyServlet implements Servlet{
    private transient ServletConfig servletConfig;

    @Override
    public void init(ServletConfig config) throws ServletException {

        this.servletConfig = config;
        //为什么控制台不会输出下面这句话
        System.out.print("########Init启动,则证明服务器开始监听4700这个端口了");
          new Thread(){
            @Override
            public void run(){
                try {
                    ServerSocket server = new ServerSocket(4700);
                    Socket socket ;
                    while(true){
                        socket = server.accept();

                        //a new thread
                        SocketServer sServer = new SocketServer(socket);

                        sServer.start();


                    }
                } catch (IOException ex) {
                    Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex);
                }//catch ended
            }
        }.start();


    }

    @Override
    public ServletConfig getServletConfig() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        PrintWriter writer = res.getWriter();
        res.setContentType("text/html");

        writer.print("<html><head></head>"+"<body>hello master</body></html>");

    }

    @Override
    public String getServletInfo() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * 功能简介:这是Socket。服务器通过它来与客户端对话 
 * @author nazi
 */
class SocketServer extends Thread {
    private final Socket  socket;
    public SocketServer(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run(){
        String output = " ";
        try{
            boolean flag = true ;
            Socket linkSocket ;
            String inputLine;
            ServerSocket myServer = new ServerSocket(4700);
            System.out.println("服务器监听开始 :" + myServer.getLocalPort());
            while(flag){
                //
                linkSocket = myServer.accept();

                try ( //get data from cilent
                        DataInputStream dis = new DataInputStream(new BufferedInputStream(linkSocket.getInputStream())); 


                        PrintStream ps = new PrintStream(new BufferedOutputStream(linkSocket.getOutputStream()))) {

                        ps.println("hello Welcome connectto my server");

                        ps.flush();

                        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

                       inputLine = in.readLine();
                       //read data from cilent

                    while(!inputLine.equals("bye")){
                        System.out.println("客户端输入的信息是:"+inputLine);
                        inputLine = in.readLine();

                    }//while ended
                }
                linkSocket.close();
                myServer.close();        
            }
        }catch(IOException e){
            System.out.println("ServerException"+e);
        }
    }

}

客户端程序

 /*
 * 客户端程序
 */
package clienttest;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 功能简介:ClientTest类为客户端程序。
 * @author nazi
 */
public class ClientTest {

    /**
     * @param args the command line arguments
     * @throws java.net.UnknownHostException
     */
    public static void main(String[] args) throws UnknownHostException, IOException {
        try{

            String line ;
            String s;

            Socket mySocket = new Socket(InetAddress.getLocalHost(),4700);
            System.out.println("客户端找到服务器");

            try {
                PrintStream os = new PrintStream(new BufferedOutputStream(mySocket.getOutputStream()));
                DataInputStream dis = new DataInputStream(mySocket.getInputStream());
                //从Socket中读数据
                s = dis.readLine();
                System.out.println(s);

                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                line = in.readLine();
                while(!line.equals("bye")){
                    os.println(line);
                    os.flush();
                    line = in.readLine();
                }//while ended
                System.out.println("客户端断开");
                dis.close();
            }catch(IOException e)
        {
            System.out.println("ClientError" + e);
        }

        }catch(IOException e)
        {
            System.out.println("ClientError" + e);
        }
    }

}

#**悬赏解答 不胜感激**#

1个回答

服务器控制台

客户端控制台

#为什么连System.out.println()语句都不输出呢#

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐