asynchronous communication instant messenger program......
I am in networking and did not expect to run into this, this is my code below. I must turn this instant messenger program into an asynchronous communication instant messenger program. What to do to make it to make it asychronous? Here is my code:
import java.io.*;
import java.net.*;
class server
{
public static void main(String[] args)
{
try {
Socket s = (new ServerSocket(2010)).accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.println(str);
(new DataOutputStream(s.getOutputStream())).writeBytes( str + "\n");
} catch (IOException e) {
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
class client
{
public static void main(String[] args)
{
try {
Socket s = new Socket("localhost", 2010);
(new DataOutputStream(s.getOutputStream())).writeBytes( "Hello, World\n");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println(e);
};
}
}
Re: asynchronous communication instant messenger program......
See the following link for a tutorial on the tools you need: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
Re: asynchronous communication instant messenger program......
Reposted with proper highlights and code tags.... O_o
Re: asynchronous communication instant messenger program......
So what do you mean by asynchronous? You can't have one run without the other... Do you mean run them within the same program?
All that requires is multi-threading. Host code in the main method and client code goes in a Runnable object, executed by a new Thread.