java socket programming problem

Posted by mk.persia on Stack Overflow See other posts from Stack Overflow or by mk.persia
Published on 2011-01-03T22:12:08Z Indexed on 2011/01/03 22:53 UTC
Read the original article Hit count: 246

Filed under:
|

Hi, what's wrong with my code? sorry about my bad English


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

    }

}

output:

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

and it waits here forever!

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets