Hey guys, I would like some input on my relatively simple Client to server messaging program
I think the Client part is OK, but the server bit is a bit broken
The program actually sort of works (client can send messages to server)
I am having trouble putting a username + password on it (set it up for a default password + username , not storing multiple usernames + password) (currently using username:1, password:2)
And I would like for the server the receive the clients Ip, when it connects, if anyone has any spare time and some java experience it would be greatly appreciated
Client:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class LongClient {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
Scanner kbReader = new Scanner(System.in);
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.flush();
String message, servermessage;
InetAddress clientip = InetAddress.getLocalHost();
System.out.println("Preparing to chat...");
out.println("Client IP Address: " + clientip); // sends message to client giving ip
out.println("Client hostname: " + clientip.getHostAddress()); // send message to client giving computer name
do{
if (in.ready()){
servermessage=in.readLine();
System.out.println("server>: "+servermessage);
}
message=kbReader.nextLine();
out.println(message);
out.println("Done");
//message="bye";
//out.println("bye");
Thread.currentThread().sleep(300);
}while (!message.equals("bye"));
out.close();
in.close();
}
catch(Exception e) {
System.out.print(e);
}
}
}
Server:
import java.lang.*;
import java.io.*;
import java.net.*;
class LongServer {
public static void main(String args[]) throws IOException {
String data = "Welcome to My Server"; //welcome message
String data1 = "enter username"; //welcome message
String message;
int idstats1 = 0; //verifyed username
int idstats2 = 0; //verifyed password
int verify1 = 0;
try {
//Detecting the localhost's ip address
InetAddress localaddr = InetAddress.getLocalHost();
System.out.println ("Local IP Address : " + localaddr );
System.out.println ("Local hostname : " + localaddr.getHostAddress());
// Creating a server socket for connection
ServerSocket srvr = new ServerSocket(1234);
System.out.println("Waiting for connection on "+localaddr);
// Accept incoming connection
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
// get Input and Output streams
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.flush();
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Sending string: '" + data + "'\n");
out.println(data ); //sends welcome message
System.out.print("Sending string: '" + data1 + "'\n");
out.println(data1); //sends welcome message
message=in.readLine(); //reads the line typed in
while(verify1==0){
if (idstats1 == 0) { //if the client is not verifyed, it asks for a password
out.println("enter username");
}
if (idstats2 == 0) { //if the client is not verifyed, it asks for a password
out.println("enter password");
}
if (message.equals("1")){ //is the input is password, then the client is verifyed
out.println("Username accepted");
idstats1=1;
}
if (message.equals("2")){ //is the input is password, then the client is verifyed
out.println("Password Accepted");
idstats2=1;
}
if (idstats1==1 && idstats2==1){
verify1=1;
}
}
if (verify1==1) { //if client is verifyed, it asks them to type a message and prints it to the console
out.println("enter a message");
System.out.println("client>"+message);
}
if (message.equals("bye")){ //if the client enters "bye" then the server closes
out.println("Server closing");
System.out.println("server>Server closing");
}
while(!message.equals("bye")); {
out.close();
skt.close();
srvr.close();}
}catch(BindException e){
e.printStackTrace();
System.out.print("A server is already running on the same port.");
}catch(SocketException e) {
e.printStackTrace();
System.out.print("Client has disconnected rudely.");
}catch(Exception e){
e.printStackTrace();
System.out.print(e);
}
}
}
EDIT: IS THERE A GOOD WAY TO PUT CODE ON THIS SITE