دا برنامج client بالStreamSocket
بس مش عارف ليه مش شغال ياريت حد يساعدني فيه
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class ClientApp extends JFrame
{
private JTextField enterField ;
private JTextArea displayArea ;
private ObjectInputStream input ;
private ObjectOutputStream output ;
private String message = "" ;
private String chatServer ;
private Socket client ;
public ClientApp(String host)
{
super("Client") ;
chatServer = host ;
enterField = new JTextField() ;
enterField.setEditable(false) ;
enterField.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
sendData(event.getActionCommand()) ;// TODO Auto-generated method stub
enterField.setText("") ;
}
}) ;
add(enterField , BorderLayout.NORTH) ;
displayArea = new JTextArea() ;
add(new JScrollPane(displayArea) ,BorderLayout.CENTER ) ;
}
//***********************runClient************************
public void runClient()
{
try
{
connectToServer() ;
getStreams() ;
processConnection() ;
}
catch(EOFException eofException)
{
displayMessage("\nClient terminated connection") ;
}
catch(IOException ioException)
{
ioException.printStackTrace() ;
}
finally
{
closeConnection() ;
}
}
//**********************processConnection********** ********
private void processConnection() throws IOException
{
setTextFieldEditable(true) ;// TODO Auto-generated method stub
do
{
try
{
message = (String) input.readObject() ;
displayMessage("\n"+message) ;
}
catch (ClassNotFoundException e)
{
displayMessage("\n Unknown object recieved") ;// TODO Auto-generated catch block
}
}while(!message.equals("server>>TERMINATED")) ;
}
//*****************setTextFieldEditable******************
private void setTextFieldEditable(final boolean b)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
enterField.setEditable(b) ;// TODO Auto-generated method stub
}
}) ;// TODO Auto-generated method stub
}
//********************getStreams****************************
private void getStreams() throws IOException
{
output = new ObjectOutputStream(client.getOutputStream()) ;// TODO Auto-generated method stub
input = new ObjectInputStream(client.getInputStream()) ;
output.flush() ;
displayMessage("\n Got I/O streams \n") ;
}
//***********************connectToServer*********************
private void connectToServer() throws IOException
{
displayMessage("Attempting connection\n") ;// TODO Auto-generated method stub
client = new Socket(InetAddress.getByName(chatServer),12345) ;
displayMessage("Connected to"+client.getInetAddress().getHostName()) ;
}
//*******************displayMessage***********************
private void displayMessage(final String string)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
displayArea.append(string) ;// TODO Auto-generated method stub
}
}) ;// TODO Auto-generated method stub
}
//**************closeConnection********************
private void closeConnection()
{
displayMessage("Terminating connection\n") ;
try
{
input.close() ;
output.close() ;
client.close() ;
}//end try
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}//end catch
// TODO Auto-generated method stub
}
//******************sendData******************
protected void sendData(String actionCommand)
{
try
{
output.writeObject("CLIENT>>"+message) ;
output.flush() ;
displayMessage("\nCLIENT>>"+message) ;
}
catch (IOException e)
{
displayArea.append("\nError writing object") ;// TODO Auto-generated catch block
}// TODO Auto-generated method stub
}
}
public class client
{
public static void main(String args[])
{
ClientApp application ;
if(args.length == 0)
application = new ClientApp("192.168.1.3") ;
else
application = new ClientApp(args[0]) ;
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
application.runClient() ;
}
}