public class Ow {
public String name ="abishkar";
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
import java.net.*;
import java.io.*;
public class TCPEchoClient
{
public static class TCPEchoReader extends Thread
{
public TCPEchoReader(ObjectInputStream objectinputstream)
{
// dataInputStream = input;
objectinputstram=objectinputstream;
active = true;
}
public void run()
{
while(active)
{
try
{
// String message = dataInputStream.readUTF();
// System.out.println("Received from server: "+message);
try {
Ow s= (Ow)objectinputstram.readObject();
System.out.println(s.name);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(IOException e)
{
System.out.println(e);
active = false;
}
}
}
public boolean active;
// public DataInputStream dataInputStream;
public ObjectInputStream objectinputstram;
}
public static void main(String[] args)
{
String address = "127.0.0.1";
int port = 8000;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
BufferedReader keyboardReader = null;
ObjectInputStream objecinputstram=null;
ObjectOutputStream objectoutputstram=null;
// Connect to the server...
try
{
socket = new Socket(address, port);
// Obtain the streams...
dataInputStream = new DataInputStream
(socket.getInputStream());
dataOutputStream = new DataOutputStream
(socket.getOutputStream());
objecinputstram = new ObjectInputStream
(socket.getInputStream());
objectoutputstram = new ObjectOutputStream
(socket.getOutputStream());
keyboardReader = new BufferedReader
(new InputStreamReader(System.in));
}
catch(IOException e)
{
System.out.println("Problems initialising: "+e);
System.exit(1);
}
try
{
// Start the listening thread...
TCPEchoReader reader = new TCPEchoReader
(objecinputstram);
reader.setDaemon(true);
reader.start();
String input;
while(true)
{
// read data in from the keyboard
input = keyboardReader.readLine();
// send data to server
dataOutputStream.writeUTF(input);
}
}
catch(IOException e)
{
}
try
{
socket.close();
}
catch(IOException e) { }
}
}
import java.net.*;
import java.io.*;
public class TCPEchoServer
{
public static void main(String args[])
{
int port = 8000;
ServerSocket serverSocket = null;
// DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
ObjectInputStream objecinputstram=null;
ObjectOutputStream objectoutputstram=null;
try
{
// open a server socket
serverSocket = new ServerSocket(port);
System.out.println("Server created on port "+port);
System.out.println("Awaiting client connection...");
// await for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected from "+clientSocket.getInetAddress());
// dataInputStream = new DataInputStream
// (clientSocket.getInputStream());
dataOutputStream = new DataOutputStream
(clientSocket.getOutputStream());
objecinputstram = new ObjectInputStream
(clientSocket.getInputStream());
objectoutputstram = new ObjectOutputStream
(clientSocket.getOutputStream());
}
catch(IOException e)
{
System.out.println("Problems initializing server: "+e);
System.exit(1);
}
// communicate with the client
try
{
// dataOutputStream.writeUTF("Welcome to the TCP Echo Server!");
objectoutputstram.writeObject(new Ow());
String input;
while(true)
{
// read data in from client
// input = dataInputStream.readUTF();
// System.out.println("You typed: "+input);
// write data back to client
// dataOutputStream.writeUTF(input);
objectoutputstram.writeObject(new Ow());
}
}
catch(IOException e)
{
System.out.println("Client disconnected from server");
}
try
{
serverSocket.close();
}
catch(Exception e) { }
}
}
No comments:
Post a Comment