繁体   English   中英

java socket编程问题

[英]java socket programming problem

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


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

两个线程都挂起while((line =br.readLine()) != null)

现在它有效


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;
            line = br.readLine();
            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!\n");
            bw.flush();
        }
        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!\n");
            bw.flush();
        }
        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;
            line =br.readLine();
            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("Client: 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
Client: stream done
Server: stream done
Server: reading the request
Client: requesting
Client: requesting finished
Client: reading the respond
Server: client said-> Hi! I am Client!
Server: reading fished
Server: responding
Server: responding finished
Server: is finishing
Client: server said-> Hi! I am server!
Client: reading fished
Client: is finishing
Client: done
Server: done
Main done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM