Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: what's wrong with my code ? " server client socket

  1. #1
    Junior Member Wish..'s Avatar
    Join Date
    Mar 2012
    Location
    sky !
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong with my code ? " server client socket

    hi ..
    This is my code with no errors .. but I didn't know why the output doesn't show up
    the program should take a message from user as a string then send it to the server , the server will count the number of digits and Characters then send the numbers as an object to the client and print it ..

    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
     
    public class TcpClient {
     
            public static void main(String argv[]) throws Exception{ 
     
      String clientSentence;
     
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    Socket clintSocket = new Socket("localhost",6780); 
    DataOutputStream outToServer =new DataOutputStream(clintSocket.getOutputStream());
    clientSentence = inFromUser.readLine(); 
    outToServer.writeBytes(clientSentence + '\n'); 
    InputStream inputStream = clintSocket.getInputStream();
    ObjectInputStream objectInput = new ObjectInputStream(inputStream);
    MessageImp1 m = (MessageImp1) objectInput.readObject();
      m.setcounts(clientSentence);  
     
       System.out.println(" The nubmer of Characters is : "+m.getCharacterCount()+ "\n" +" The nubmer of Digits is : "+m.getDigitCount());
       clintSocket.close();
     
    }
    }



    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
     
    public class TCPServer {
     
        public static void main(String argv[]) throws Exception{ 
              String clientSentence;
     
     ServerSocket welcomeSocket = new ServerSocket(6780); 
     while(true) {
     Socket connectionSocket = welcomeSocket.accept(); 
     
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
    clientSentence = inFromClient.readLine(); 
     
    MessageImp1 m1=new MessageImp1();
    m1.setcounts(clientSentence);
    ObjectOutputStream ooStream = new ObjectOutputStream(connectionSocket.getOutputStream());
     
    ooStream.writeObject(m1);
    ooStream.close();
     
     
    }
        }}


     
     
    public interface Message {
     
        public void setcounts(String str);
        public  int getCharacterCount();
        public  int getDigitCount();
     
     
    }

    import java.io.Serializable;
     
     
    public class MessageImp1 implements Message,Serializable{
     
    int lettersCount=0;
    int digitCount=0;
     
     
     
     
    public void setcounts(String str) {
          for(int i=0;i<str.length();i++){
        if(Character.isLetter(str.charAt(i))){
        lettersCount++; }
          else if (Character.isDigit(str.charAt(i))){
        digitCount++;}
          }
    } 
     
     
        public int getCharacterCount() {
     
        return lettersCount;
        }
     
        @Override
        public int getDigitCount() {
         return digitCount;
        }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,910
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: what's wrong with my code ? " server client socket

    what happens when the Program executes?
    Try debugging the code by Adding some println statements to show where it is executing and the values of variables as they are changed.

    Edit the first two classes and fix the formatting. Its lack of proper indentations makes it hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Wish..'s Avatar
    Join Date
    Mar 2012
    Location
    sky !
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my code ? " server client socket

    Thank you .. the program is working now : )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,910
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: what's wrong with my code ? " server client socket

    Then mark it as Solved
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. server/client application fails when client closes
    By billykid in forum Java Networking
    Replies: 4
    Last Post: January 26th, 2012, 01:54 AM
  2. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  3. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  4. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 08:00 AM
  5. Replies: 1
    Last Post: March 31st, 2010, 10:42 PM