0
import java.io.*;
import java.net.*;

public class Server 
{
ServerSocket ss;
ObjectOutputStream oos;
ObjectInputStream ois;
public Server()
{
try
{
ss = new ServerSocket(8888);
}
catch(IOException e)
{
e.printStackTrace();
}
}

public void run()
{	
String message;
try
{
Socket sock = ss.accept();
System.out.println ("Connection received from " + sock.getInetAddress().getHostName());
oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject("Welcome....");
ois = new ObjectInputStream(sock.getInputStream());
try
{
message = (String)ois.readObject();
System.out.println ("Client >>" + message);
}
catch(ClassNotFoundException cn)
{
System.err.println ("Data received in unknown format");
}

finally
{
try
{
ois.close();
oos.close();
sock.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}

public void sendMessage(String mssg)
{
try
{
oos.writeObject(mssg);
oos.flush();
System.out.println ("Server >> " + mssg);
}
catch(IOException e)
{
e.printStackTrace();
}
}

public static void main(String[]args)
{
Server serv = new Server();
while(true)
{
serv.run();
}
}
}
import java.io.*;
import java.net.*;

public class User 
{
Socket s;
ObjectOutputStream oos;
ObjectInputStream ois;
public User()
{
try
{
s = new Socket("localhost", 8888);
}
catch(IOException e)
{
e.printStackTrace();
}
}

public void run()
{
String message;
try
{
oos = new ObjectOutputStream(s.getOutputStream());
ois = new ObjectInputStream(s.getInputStream());
try
{
message = (String)ois.readObject();
System.out.println ("Server >> " + message);
}
catch(ClassNotFoundException cn)
{
System.err.println ("Data received in unknown format");
}
}
catch(UnknownHostException uh)
{
System.err.println ("You are trying to connect to an unknown host");
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
oos.close();
ois.close();
s.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

public void sendMessage(String mssg)
{
try
{
oos.writeObject(mssg);
oos.flush();
System.out.println ("Client >> " + mssg);
}
catch(IOException e)
{
e.printStackTrace();
}
}

public static void main(String[]args)
{
User use = new User();
while(true)
{
use.run();
}
}
}

please help me fix this program..

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by Ezzaral
0

What is the problem ? And where ?

it have proses completed but it won't work..
can you fix it?

0

Please be more specific about what you are having troubles with.

Also, learn to indent your code because that is a monolithic mess you have posted and no one wants to read that.

Edited by Ezzaral: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.