java socket编程问题

标签 java sockets

我的代码有什么问题? 抱歉我的英语不好


package sockettest;

import java.io.*;
import java.net.*;

class sevr implements Runnable{
    public void run() {
        ServerSocket sSkt = null;
        Socket skt = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try{
            System.out.println("Server: is about to create socket");
            sSkt = new ServerSocket(6666);
            System.out.println("Server: socket created");
        }
        catch(IOException e){
            System.out.println("Server: socket creation failure");
        }
        try{
            System.out.println("Server: is listening");
            skt = sSkt.accept();
            System.out.println("Server: Connection Established");
        }
        catch(IOException e){
            System.out.println("Server: listening failed");
        }
        try{
            System.out.println("Server: creating streams");
            br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
            System.out.println("Server: stream done");
        }
        catch(IOException e){
            System.out.println("Server: stream failed");
        }
        System.out.println("Server: reading the request");
        try{
            String line = null;
            while((line =br.readLine()) != null){
            System.out.println("Server: client said-> "+ line);
            }
        }
        catch(IOException e){
            System.out.println("Server: reading failed");
        }
        System.out.println("Server: reading fished");

        System.out.println("Server: responding");
        try{
            bw.write("Hi! I am server!");
        }
        catch(IOException e){
            System.out.println("Server: responding failed");
        }
        System.out.println("Server: responding finished");

        System.out.println("Server: is finishing");
        try {
            br.close();
            bw.close();
            skt.close();
            sSkt.close();
        } catch (IOException e) {
            System.out.println("Server: finishing failed");
        }
        System.out.println("Server: done");
    }
}

class clnt implements Runnable{
    public void run() {
        Socket skt = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try{
            System.out.println("Client: about to create socket");
            skt = new Socket(InetAddress.getLocalHost(),6666);
            System.out.println("Client: socket created");
        }
        catch(IOException e){
            System.out.println("Client: socket creation failure");
        }

        try{
            System.out.println("Client: creating streams");
            br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
            System.out.println("Client: stream done");
        }
        catch(IOException e){
            System.out.println("Client: stream failed");
        }
        System.out.println("Client: requesting");
        try{
            bw.write("Hi! I am Client!");
        }
        catch(IOException e){
            System.out.println("Client: requesting failed");
        }
        System.out.println("Client: requesting finished");
        System.out.println("Client: reading the respond");
        try{
            String line = null;
            while((line =br.readLine()) != null){
            System.out.println("Client: server said-> "+ line);
            }
        }
        catch(IOException e){
            System.out.println("Client: reading failed");
        }
        System.out.println("Client: reading fished");



        System.out.println("Clientrver: is finishing");
        try {
            br.close();
            bw.close();
            skt.close();
        } catch (IOException e) {
            System.out.println("Client: finishing failed");
        }
        System.out.println("Client: done");
    }
}


public class Main {


    public static void main(String[] args) {
        System.out.println("Main started");
        Thread sThread = new Thread(new sevr());
        Thread cThread = new Thread(new clnt());
        sThread.start();
        cThread.start();
        try {
            sThread.join();
            cThread.join();
        } catch (InterruptedException ex) {
            System.out.println("joining failed");
        }
        System.out.println("Main done");

    }

}

输出:

Main started
Server: is about to create socket
Client: about to create socket
Client: socket created
Client: creating streams
Server: socket created
Server: is listening
Server: Connection Established
Server: creating streams
Server: stream done
Server: reading the request
Client: stream done
Client: requesting
Client: requesting finished
Client: reading the respond

它永远在这里等待!

最佳答案

程序正在使用 BufferedWriter 但它没有被刷新(它将位于缓冲区中直到被刷新、缓冲区填满或关闭)并且没有发送换行符(readLine 将等待直到你发送一个换行符)

尝试

bw.write("I am the Client!!\n");
bw.flush();

关于java socket编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4588852/

相关文章:

java - Lombok项目是否可以返回对象的接口(interface)?

java - @Transactional Spring 不创建新事务

java - Tomcat 8 上下文初始化两次

python - 如何在 python 中通过 udp (DTLS) 套接字实现 http/3 协议(protocol)?

java - socket.isConnected() 不检测断开连接的套接字

Python,为什么在使用 TCP 套接字时出现错误 10035(在服务器上)和 10053(在客户端上)?

java - 如何用box shadow做相对布局

Java 压缩 try/catch 代码异常

java - 在单个上下文切换中将相同的 tcp 消息发送到多个目的地/主机

c - 一次将数据写入多个套接字(一个系统调用)