java socket编程问题

时间:2011-01-03 22:12:08

标签: 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

它永远等在这里!

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

两个主题都挂在while((line =br.readLine()) != null)

答案 2 :(得分:-1)

现在可行了


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