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..

Recommended Answers

All 3 Replies

What is the problem ? And where ?

What is the problem ? And where ?

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.