Created
January 20, 2012 10:11
-
-
Save jeslyvarghese/1646545 to your computer and use it in GitHub Desktop.
chat client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
public class ChatClient extends Thread | |
{ | |
static Socket clientsocket = null; | |
BufferedReader fromServer = null; | |
String recieveddata = null; | |
ChatClient() | |
{ | |
this.start(); | |
} | |
public void run() | |
{ | |
try{ | |
this.sleep(1000); | |
} | |
catch(Exception e) | |
{ | |
} | |
try | |
{ | |
fromServer = new BufferedReader(new InputStreamReader(clientsocket.getInputStream())); | |
} | |
catch(Exception e) | |
{ | |
System.out.println("Fail!"); | |
} | |
do{ | |
try{ | |
recieveddata = fromServer.readLine(); | |
System.out.println(recieveddata); | |
} | |
catch(Exception e) | |
{ | |
System.exit(0); | |
} | |
}while(true); | |
} | |
public static void main(String args[])throws Exception | |
{ | |
String Name = new String(args[0]); | |
String sentdata; | |
BufferedReader inputReader = null; | |
PrintWriter toServer = null; | |
inputReader = new BufferedReader(new InputStreamReader(System.in)); | |
clientsocket = new Socket("localhost",6060); | |
toServer = new PrintWriter(clientsocket.getOutputStream(),true); | |
new ChatClient(); | |
do | |
{ | |
sentdata = inputReader.readLine(); | |
toServer.println(Name+":"+sentdata); | |
} | |
while(!sentdata.equals("exit")); | |
clientsocket.close(); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment