Client to Server Messaging- Java

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

pastebin

Like the previous responder said, instead of pasting your code in a post, you should create a new pastebin page(s), and put the links to your pastebin pages in your post.  It is super easy to use pastebin and you do not even need to create a user account to use pastebin.

It looks like you are using Java 6 for your development, you should upgrade to Java 7 or Java 8 and use try with resources instead of simple try and catch blocks. 

The main thing wrong with your code is client/server synchronization.  You have to make sure your client reads match with server writes and vice verse.  Your initial client/server “handshake” appears to be the main problem, but there were other synchronization problems as well and your user authentication also had an issue.

Since it looks like you are doing this as a learning exercise, I left as much of your code as possible intact.  If I made changes I broke things out into their own methods.  I had to make these methods static since you are doing everything in main.

I do have a couple of couple of quick comments.  Remember Java has Boolean variables so you do not have to use int values for true and false.  When you close resources, you need to do this in a finally block, otherwise if exceptions occur, resources may not be cleaned up properly.  You should refactor your client and server, moving all of the functionality into discrete methods and use main only as a “test driver.”  Right now your server only handles one single client.  Typically, when a server receives a client connection you spawn a new thread to handle client communication.  Take a look at Java’s Executors for easy and efficient methods to do this.  You can also look at using Java’s “New I/O,” NIO, to create a client and server just to get a feel for how it compares to traditional I/O in Java.  NIO is better suited for handling many concurrent clients sending small amounts of data, like a chat application.

Modified Client:

http://pastebin.com/BQr18WCL

Modified Server:

http://pastebin.com/eNzmtkG3

same vote from me here, use pastebin man. as for sending data around, spawn off a thread for a client manager and them have it consistently update clients. on the client side have a thread to handle the server comms. that way it can constantly be checking for data from the server and then when received send it off to some other mechanism. definately break things down to specific entities. that is the point of OOP! make an object very specific in its task.