2012-08-02 2 views
-1

i un serveur qui est la suivante:connexion serveur TCP reset

import java.io.*; 
import java.net.*; 
import java.util.StringTokenizer; 

public class MyTcpServer { 

    static ServerSocket server = null; 
    static Socket connectionSocket = null; 
    static BufferedReader inFromClient = null; 
    static PrintWriter outToClient = null; 

    public static void initiateService() { 
     try { 
      server = new ServerSocket(1234); 

      System.out.println("TCPServer Waiting for client on port 1234"); 
     } catch (IOException e) { 
      System.out 
        .println("There is some error!! Please try some other port!"); 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void awaitRequest() throws IOException { 
     String instructionsToClient = null; 
     System.out.println("Service Ready"); 
     while (true) { 
      connectionSocket = server.accept(); 

      inFromClient = new BufferedReader(new InputStreamReader(
        connectionSocket.getInputStream())); 
      outToClient = new PrintWriter(connectionSocket.getOutputStream(), 
        true); 

      String messageToClient; 
      String messageFromClient = null; 

      synchronized (Thread.currentThread()) { 

       System.out.println("Client:" 
         + connectionSocket.getInetAddress() 
         + " connected on port :" 
         + connectionSocket.getLocalPort()); 

       messageToClient = "Welcome!!!! You are now connected to our server!! We ensure a reliable service.\n" 
         + "\n Your IP:" 
         + connectionSocket.getInetAddress() 
         + " \n" 
         + connectionSocket.getLocalPort() 
         + "::::MESSAGE FROM SERVER::::\n" 
         + "Lets play Water jug puzzel!! You give me sizes of two water Jars\n" 
         + "And a desired level to be achieved! Ill tell you whether the problem\n" 
         + "is solvable or not!!\n" 
         + "Comprande~?\n" 
         + "Enter your choice Y/N \n" 
         + "--------------------------"; 

       synchronized (outToClient) { 

        outToClient.println(messageToClient); 
        outToClient.flush(); 

        System.out.println("invite sent to the client!!"); 

       } 

       synchronized (inFromClient) { 

        messageFromClient = inFromClient.readLine(); 
        System.out.println("The user says: " + messageFromClient); 

       } 

       if (messageFromClient.equalsIgnoreCase("y")) { 
        messageToClient = "Wow!! awesome! Throw a challenge at me!" 
          + "\nEnter Large jug volume,Small jug volume,Desired level in comma separated fashion" 
          + "\nFor example (5,3,2)" 
          + "\n--------------------------"; 

       } else if (messageFromClient.equalsIgnoreCase("n")) { 
        messageToClient = "Great! we dont have a problem with you not playing the puzzel!" 
          + "Do you wanna exit? press q and smack Enter key!" 
          + "\n--------------------------"; 
       } else { 
        messageToClient = "psssst!!! Wanna quit??" 
          + "press q and smack Enter key!" 
          + "\n--------------------------"; 
       } 

       synchronized (messageToClient) { 

        outToClient.println(messageToClient); 
        outToClient.flush(); 

        System.out.println("reply sent to the client!!"); 

       } 
       messageFromClient = ""; 
       synchronized (inFromClient) { 
        System.out.println("Entered"); 

        messageFromClient = inFromClient.readLine(); 
        System.out.println("The user says: " + messageFromClient); 



       if (messageFromClient.equalsIgnoreCase("q")) { 
        System.out.println("ui"); 
        messageToClient = "Thank you!!" 
          + "\n--------------------------"; 
       } else { 
        StringTokenizer st = new StringTokenizer(messageFromClient, 
          ","); 

        if (st.countTokens() != 3) { 
         messageToClient = "See! the input you gave has some problem!" 
           + "\n you havent followed the instructions properly" 
           + "\n Thank you!! Bye for now! come back later!" 
           + "\n--------------------------"; 
        } else { 
         messageToClient = "Thanks! i am solving it!" 
           + "\n--------------------------"; 


        } 
       } 
       } 

       synchronized (messageToClient) { 

        outToClient.println(messageToClient); 
        outToClient.flush(); 

        System.out.println("problem answered sent to the client!!"); 

       } 

      } 
     } 
    } 



    public static void main(String[] args) throws IOException { 
     initiateService(); 
     awaitRequest(); 
    } 

} 

et j'ai un client qui accède à ce serveur!

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

public class MyTcpClient { 

    static Socket clientSocket; 
    static BufferedReader inFromUser; 
    static PrintWriter outToServer; 
    static BufferedReader inFromServer; 

    public static void main(String argv[]) throws Exception { 
     String FromServer; 
     String ToServer; 

     clientSocket = new Socket("localhost", 1234); 

     // for user to type messages in client mode 
     inFromUser = new BufferedReader(new InputStreamReader(System.in)); 

     // client program uses this to write on the server 
     outToServer = new PrintWriter(clientSocket.getOutputStream(), true); 

     // servers talk back to the client 
     inFromServer = new BufferedReader(new InputStreamReader(
       clientSocket.getInputStream())); 

     FromServer = ""; 
     while (!FromServer.equals("--------------------------")) { 
      FromServer = readFromServer(); 
      System.out.println(FromServer); 

     } 


     System.out.println("Enter choice!"); 
     String choice = ""; 

     choice = inFromUser.readLine(); 
     System.out.println("" + choice); 

     synchronized (outToServer) { 

      outToServer.println(choice); 
      outToServer.flush(); 
     } 

     FromServer = ""; 
     while (!FromServer.equals("--------------------------")) { 
      FromServer = readFromServer(); 
      System.out.println(FromServer); 

     } 

     choice = inFromUser.readLine(); 
     System.out.println("" + choice); 

     synchronized (outToServer) { 

      outToServer.println(choice); 
      outToServer.flush(); 
     } 

     FromServer = ""; 
     while (!FromServer.equals("--------------------------")) { 
      FromServer = readFromServer(); 
      System.out.println(FromServer); 
     } 

    } 

    static String readFromServer() throws IOException { 
     String response = inFromServer.readLine(); 
     return response; 
    } 

} 

sur une course réussie la sortie du côté du serveur est quelque chose comme ceci:

TCPServer Waiting for client on port 1234 
Service Ready 
Client:/127.0.0.1 connected on port :1234 
invite sent to the client!! 
The user says: y 
reply sent to the client!! 
Entered 
The user says: 5,3,2 

et la sortie du côté client est quelque chose comme ceci:

Welcome!!!! You are now connected to our server!! We ensure a reliable service. 

Your IP:/127.0.0.1 
1234::::MESSAGE FROM SERVER:::: 
Lets play Water jug puzzel!! You give me sizes of two water Jars 
And a desired level to be achieved! Ill tell you whether the problem 
is solvable or not!! 
Comprande~? 
Enter your choice Y/N 
-------------------------- 
Enter choice! 
y 
y 
Wow!! awesome! Throw a challenge at me! 
Enter Large jug volume,Small jug volume,Desired level in comma separated fashion 
For example (5,3,2) 
-------------------------- 
5,3,2 
5,3,2 
Exception in thread "main" java.net.SocketException: Connection reset 
    at java.net.SocketInputStream.read(SocketInputStream.java:168) 
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) 
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) 
    at java.io.InputStreamReader.read(InputStreamReader.java:167) 
    at java.io.BufferedReader.fill(BufferedReader.java:136) 
    at java.io.BufferedReader.readLine(BufferedReader.java:299) 
    at java.io.BufferedReader.readLine(BufferedReader.java:362) 
    at MyTcpClient.readFromServer(MyTcpClient.java:85) 
    at MyTcpClient.main(MyTcpClient.java:78) 

Exception est soulevé ici est seulement quand une chaîne séparée par des virgules est fournie. Ne pas avoir une idée de ce qui ne va pas, quelqu'un peut-il partager quelque chose?

+0

Votre code fonctionne très bien pour moi, pas d'exceptions –

+0

Même ici, fonctionne bien sur ma fin – MadProgrammer

+0

quand j'entre dans 5,3,2 ou quelque chose comme ça encore l'erreur persiste :( – shridatt

Répondre

1

Vous avez des autres problèmes dans votre code:

static ServerSocket server = null; 

ne doit pas être statique.

static Socket connectionSocket = null; 

Ne doit pas être statique ni même un membre d'instance. Ce devrait être une variable locale dans la méthode qui appelle accept().

static BufferedReader inFromClient = null; 
static PrintWriter outToClient = null; 

Ditto dans ce cas, mais dans le cas général, ils devraient être membres d'instance d'une classe par connexion que vous instancier pour chaque prise récemment acceptée. Il s'agit généralement d'un Runnable, dont la méthode run() effectue toutes les E/S pour ce client ou appelle les méthodes qui le font.

+0

J'ai essayé de faire tous les changements comme vous l'avez dit ici, mais toujours la même chose! – shridatt

1

Je n'allais pas essayer votre code, mais comme deux personnes l'ont déjà fait, je pense que peut-être le problème n'est pas votre code.

Jetez un coup d'œil à this.