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 ..
Code :
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();
}
}
Code :
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();
}
}}
Code :
public interface Message {
public void setcounts(String str);
public int getCharacterCount();
public int getDigitCount();
}
Code :
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;
}
}
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.
Re: what's wrong with my code ? " server client socket
Thank you .. the program is working now : )
Re: what's wrong with my code ? " server client socket