السلام عليكم ورحمه الله و بركاته
كان لي استفسار بسيط بخصوص الSocket
انا قمت بعمل سيرفر و كلاينت باستخدام السوكت و اشتغل معايا
ولكنه يشتغل علي جهازين يكونا علي نفس الروتر و اشتغل ايضا علي اللوكال هوست
ولكن اذا كان الجهازين ليسا علي نفس الروتلا لا يحدث اتصال
هذا هوا كود السيرفر
/*
* WRITTEN BY ALAA ATTYA :)
* VIDO FOR IT WORKS
* ENJOY IT :)
*
* */
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.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Server extends JFrame
{
private JTextField enterField ;
private JTextArea displayArea ;
private ObjectOutputStream output ;
private ObjectInputStream input ;
private ServerSocket server ;
private Socket connection ;
private int counter = 1 ;
//******************Server GUI and actions*************
public Server()
{
super("Server") ;
enterField = new JTextField() ;
enterField.setEditable(false) ;
displayArea = new JTextArea() ;
enterField.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
sendData(event.getActionCommand()) ;
enterField.setText("") ;// TODO Auto-generated method stub
}
}) ;
add(enterField , BorderLayout.NORTH) ;
add(new JScrollPane(displayArea), BorderLayout.CENTER) ;
displayArea.setEditable(false);
}
//**************Server Methods*****************
public void runSrver()
{
try
{
server = new ServerSocket(12345,100) ;
while(true)
{
try
{
waitForConnection() ;
getStreams() ;
processConnection() ;
}
catch(EOFException eof)
{
displayMessage("\n SERVER TERMINATED CONNECTION") ;
}
finally
{
closeConnection() ;
counter++ ;
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//***************Methods Bodies*************
//***********waitForConnection Method Implementation (Step : 1 )********
private void waitForConnection() throws IOException
{
displayMessage("Waiting for connection\n") ;// TODO Auto-generated method stub
connection = server.accept() ;//Step 2 : The server listens to the client
displayMessage("Connection"+counter+"Recieved from"+connection.getInetAddress().getHostName());
}
//***********getStreams Method Implementation(Step : 3 )************
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream()) ;// TODO Auto-generated method stub
output.flush() ;
input = new ObjectInputStream(connection.getInputStream()) ;
displayMessage("\n Got I/O Streams\n") ;
}
//***********processConnection Method Implementation*********
private void processConnection() throws IOException
{
String message = "Connection Successful " ;// TODO Auto-generated method stub
sendData(message) ;
setTextFieldEditable(true) ;
do
{
try
{
message = (String)input.readObject();
displayMessage('\n'+message) ;
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
displayMessage("\n UNKNOW OBJECT TYPE RECIEVED") ;
}
}while(!message.equals("CLIENT>>terminate")) ;
}
//*******************closeConnection Method Implementation***************
private void closeConnection()
{
displayMessage("\n Terminating Connection.....") ;// TODO Auto-generated method stub
setTextFieldEditable(false) ;
try
{
output.close() ;
input.close() ;
connection.close() ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//******************setTextEditable Method Implementation*********************
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
}
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
}
//**************sendData Method Implementation****************
private void sendData(final String message)
{
try
{
output.writeObject("SERVER>>"+message) ;
output.flush() ;
displayMessage("\nSERVER>>"+message) ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
displayArea.append("\nERROR WRITTING OBJECT") ;
}
}
public static void main(String args[])
{
Server serverApp = new Server() ;
serverApp.setVisible(true) ;
serverApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
serverApp.setSize(300, 300) ;
serverApp.setLocationRelativeTo(null) ;
serverApp.runSrver() ;
}
}وهذا هو كود الكلاينت
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;
public class Client extends JFrame
{
private JTextField enterField ;
private JTextArea displayArea ;
private Socket client ;
private ObjectInputStream input ;
private ObjectOutputStream output ;
private String chatServer ;
public Client(String host)
{
super("Client") ;
chatServer = host ;
enterField = new JTextField() ;
//enterField.setToolTipText("biga");
enterField.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent event)
{
sendData(event.getActionCommand()) ;// TODO Auto-generated method stub
enterField.setText("") ;
}
}) ;
displayArea = new JTextArea() ;
add(new JScrollPane(displayArea)) ;
add(enterField , BorderLayout.NORTH) ;
setSize(300, 300) ;
setVisible(true) ;
setTextFieldEditable(false) ;
}
//*******************Methods Implementations************
//*******************runClient Method Implementation*******
public void runClient()
{
try
{
connectToServer() ;
getStreams() ;
processConnection() ;
}
catch(EOFException eof)
{
displayMessage("\n CLIENT TERMINATED CONNECTION") ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
//closeConnection() ;
}
}
//*******************connectToServer Method Implementation************
private void connectToServer() throws IOException
{
displayMessage("ATTEMPTING CONNECTION\n") ;
client = new Socket(InetAddress.getByName(chatServer),12345) ;
displayMessage("CONNECTED TO"+client.getInetAddress().getHostName()) ;
}
//******************getStreams Method Implementation***********
public void getStreams() throws IOException
{
output = new ObjectOutputStream(client.getOutputStream()) ;
output.flush() ;
input = new ObjectInputStream(client.getInputStream()) ;
displayMessage("\n GOT I/O STREAMS \n") ;
}
//*******************processConnection Method Implementation*********
private void processConnection()
{
String message = "CONNECTED TO SERVER \n" ;
setTextFieldEditable(true) ;
do{
try
{
message = (String) input.readObject() ;
displayMessage("\n"+message) ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
displayMessage("\nUNKONWN OBJECT TYPE RECIEVED") ;
}
}while(!message.equals("CLIENT>>terminate")) ;
}
//*****************closeConnection Method Implementation********
private void closeConnection()
{
displayMessage("\n CLOSING CONNECTION ..........") ;
try
{
output.close() ;
input.close() ;
client.close() ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
displayMessage("\n UNABLE TO CLOSE CONNECTION") ;
}
}
//**************setTextFieldEditable Method Implementation*******
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
}
//*****************displayMessage Method Implementation********
private void displayMessage(final String message)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
displayArea.append(message) ;// TODO Auto-generated method stub
}
}) ;// TODO Auto-generated method stub
}
//*******************sendData Method Implementation**********
protected void sendData(String message)
{
try
{
output.writeObject("CLIENT>>"+message) ;
output.flush() ;
displayMessage("\nCLIENT>>"+message) ;
}
catch (IOException e)
{
// TODO Auto-generated catch block
displayArea.append("\nERROR WRITTING OBJECT") ;
}// TODO Auto-generated method stub
}
public static void main(String args[])
{
Client application; // declare client application
// if no command line args
if ( args.length == 0 )
application = new Client( "127.0.0.1" );
else
application = new Client( args[ 0 ] );
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.runClient(); // run client application
}
}