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; } }