How do I set up the web server for this server.java so that a client can connect to server by its ip?Sorry I can't type Chinese now. Can you give me a basic idea?These two apps only work in LAN, how do I make it work through internet?Code://serverimport java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.util.Random;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.border.*;public class Server extends Frame implements ActionListener, WindowListener
{
static ServerSocket listenSocket;
static Socket connection;
private static int maxConnections=0;
//final JFileChooser fc = new JFileChooser();
static String message;
static TextField hostDisplay, portDisplay;
static TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button sendButton, quitButton;
public static final int DEFAULT_PORT = 8901;
static String host;
static int port;
public Server ( String s)
{
super ( s );
buildUI ();
} // end Server constructor
public void connectClient ( )
{
if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );
else port = DEFAULT_PORT;
} // end connectClient
public static void main ( String [ ] args )
{ Server serverFrame = new Server( "Server" );
serverFrame.addWindowListener ( serverFrame );
int i=0;
if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );////
else port = DEFAULT_PORT;////
try
{
ServerSocket listener = new ServerSocket(port);
Socket server;
logDisplay.setText ( "Server running on "+host+", port "+port+"\n" );/////
while((i++ < maxConnections) || (maxConnections == 0))
{
doComms connection;
server = listener.accept();
System.out.println("Connected: " );
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
t.start();
}
}
catch (IOException ioe)
{
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
} } // end main
public void buildUI ()
{
try
{
InetAddress here = InetAddress.getLocalHost ();
host = here.getHostName ();
}
catch (UnknownHostException e)
{ ;}
hostDisplay = new TextField ( host, 30 );
hostDisplay.setEditable ( false );
portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
topPanel = new Panel ();
topPanel.setLayout ( new GridLayout ( 2, 1 ) );
topPanel.add ( hostDisplay );
topPanel.add ( portDisplay );
logDisplay = new TextArea ( 40, 10 );
msgDisplay = new TextArea ( 40, 10 );
middlePanel = new Panel ();
middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
middlePanel.add ( logDisplay );
middlePanel.add ( msgDisplay );
sendButton = new Button ( "Send File" );
quitButton = new Button ( "Quit" );
sendButton.addActionListener ( this );
quitButton.addActionListener ( this );
buttonPanel = new Panel ( );
buttonPanel.add ( sendButton );
buttonPanel.add ( quitButton );
add ( "North", topPanel );
add ( "Center", middlePanel );
add ( "South", buttonPanel );
resize ( 400, 450 );
setVisible ( true );
} // end buildUI
//**** ActionListener methods public void actionPerformed ( ActionEvent e )
{
Object s = e.getSource();
// *** process Button actions
if ( s instanceof Button )
{
if ( s == sendButton )
{
/* int returnVal = fc.showOpenDialog(Server.this); logDisplay.append("Opening - "); if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile(); logDisplay.append(file.getName() + "\n"); //Create a client, give it the chosen filename, and send the data across
if (send(file.getPath().toString()))
logDisplay.append("Success." + "\n");
else
logDisplay.append("Failure." + "\n"); } // end sendButton*/
}
if ( s == quitButton )
{
hide ();
dispose ();
System.exit ( 0 );
} // end quitButton
} // end process Button actions
} // end actionPerformed //**** WindowListener methods
public void windowActivated ( WindowEvent e )
{
}
public void windowDeactivated ( WindowEvent e )
{
}
public void windowOpened ( WindowEvent e )
{
}
public void windowClosed ( WindowEvent e )
{
}
public void windowClosing ( WindowEvent e )
{
hide ();
dispose ();
System.exit(0);
}
public void windowIconified ( WindowEvent e )
{
}
public void windowDeiconified ( WindowEvent e )
{
}
} // end Server
class doComms implements Runnable
{
private Socket server;
private String line,input;
Random rand = new Random();
final String OUTPUTFILENAME = "C:\\receivedData";
doComms(Socket server)
{
this.server=server;
} public void run ()
{
try
{
FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);
BufferedOutputStream out = new BufferedOutputStream(fos);
BufferedInputStream in = new BufferedInputStream( server.getInputStream() );
//Read, and write the file to the socket
int i;
while ((i = in.read()) != -1)
{
out.write(i);
//Server.logDisplay.appendText(""+i);
//Server.logDisplay.appendText("Receiving data..."+"\n");
}
out.flush();
//out1.flush();
//out2.flush();
in.close();
//out1.close();
out.close();
//out2.close();
server.close();
Server.logDisplay.appendText("Transfer complete. File is saved to C:recievedData");
}
catch(Exception e)
{ Server.logDisplay.appendText("Error! It didn't work! " + e + "\n");
} //Sleep...
try {
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
Server.logDisplay.appendText("Interrupted");
}
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.util.Random;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.border.*;public class Server extends Frame implements ActionListener, WindowListener
{
static ServerSocket listenSocket;
static Socket connection;
private static int maxConnections=0;
//final JFileChooser fc = new JFileChooser();
static String message;
static TextField hostDisplay, portDisplay;
static TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button sendButton, quitButton;
public static final int DEFAULT_PORT = 8901;
static String host;
static int port;
public Server ( String s)
{
super ( s );
buildUI ();
} // end Server constructor
public void connectClient ( )
{
if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );
else port = DEFAULT_PORT;
} // end connectClient
public static void main ( String [ ] args )
{ Server serverFrame = new Server( "Server" );
serverFrame.addWindowListener ( serverFrame );
int i=0;
if ( ! ( portDisplay.getText () ).equals ( "" ) ) port = Integer.parseInt ( portDisplay.getText () );////
else port = DEFAULT_PORT;////
try
{
ServerSocket listener = new ServerSocket(port);
Socket server;
logDisplay.setText ( "Server running on "+host+", port "+port+"\n" );/////
while((i++ < maxConnections) || (maxConnections == 0))
{
doComms connection;
server = listener.accept();
System.out.println("Connected: " );
doComms conn_c= new doComms(server);
Thread t = new Thread(conn_c);
t.start();
}
}
catch (IOException ioe)
{
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
} } // end main
public void buildUI ()
{
try
{
InetAddress here = InetAddress.getLocalHost ();
host = here.getHostName ();
}
catch (UnknownHostException e)
{ ;}
hostDisplay = new TextField ( host, 30 );
hostDisplay.setEditable ( false );
portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
topPanel = new Panel ();
topPanel.setLayout ( new GridLayout ( 2, 1 ) );
topPanel.add ( hostDisplay );
topPanel.add ( portDisplay );
logDisplay = new TextArea ( 40, 10 );
msgDisplay = new TextArea ( 40, 10 );
middlePanel = new Panel ();
middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
middlePanel.add ( logDisplay );
middlePanel.add ( msgDisplay );
sendButton = new Button ( "Send File" );
quitButton = new Button ( "Quit" );
sendButton.addActionListener ( this );
quitButton.addActionListener ( this );
buttonPanel = new Panel ( );
buttonPanel.add ( sendButton );
buttonPanel.add ( quitButton );
add ( "North", topPanel );
add ( "Center", middlePanel );
add ( "South", buttonPanel );
resize ( 400, 450 );
setVisible ( true );
} // end buildUI
//**** ActionListener methods public void actionPerformed ( ActionEvent e )
{
Object s = e.getSource();
// *** process Button actions
if ( s instanceof Button )
{
if ( s == sendButton )
{
/* int returnVal = fc.showOpenDialog(Server.this); logDisplay.append("Opening - "); if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile(); logDisplay.append(file.getName() + "\n"); //Create a client, give it the chosen filename, and send the data across
if (send(file.getPath().toString()))
logDisplay.append("Success." + "\n");
else
logDisplay.append("Failure." + "\n"); } // end sendButton*/
}
if ( s == quitButton )
{
hide ();
dispose ();
System.exit ( 0 );
} // end quitButton
} // end process Button actions
} // end actionPerformed //**** WindowListener methods
public void windowActivated ( WindowEvent e )
{
}
public void windowDeactivated ( WindowEvent e )
{
}
public void windowOpened ( WindowEvent e )
{
}
public void windowClosed ( WindowEvent e )
{
}
public void windowClosing ( WindowEvent e )
{
hide ();
dispose ();
System.exit(0);
}
public void windowIconified ( WindowEvent e )
{
}
public void windowDeiconified ( WindowEvent e )
{
}
} // end Server
class doComms implements Runnable
{
private Socket server;
private String line,input;
Random rand = new Random();
final String OUTPUTFILENAME = "C:\\receivedData";
doComms(Socket server)
{
this.server=server;
} public void run ()
{
try
{
FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);
BufferedOutputStream out = new BufferedOutputStream(fos);
BufferedInputStream in = new BufferedInputStream( server.getInputStream() );
//Read, and write the file to the socket
int i;
while ((i = in.read()) != -1)
{
out.write(i);
//Server.logDisplay.appendText(""+i);
//Server.logDisplay.appendText("Receiving data..."+"\n");
}
out.flush();
//out1.flush();
//out2.flush();
in.close();
//out1.close();
out.close();
//out2.close();
server.close();
Server.logDisplay.appendText("Transfer complete. File is saved to C:recievedData");
}
catch(Exception e)
{ Server.logDisplay.appendText("Error! It didn't work! " + e + "\n");
} //Sleep...
try {
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
Server.logDisplay.appendText("Interrupted");
}
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.border.*;public class Client extends Frame implements ActionListener, WindowListener
{
Socket connection;
//static final String OUTPUTFILENAME = "C:\\receivedData";
final JFileChooser fc = new JFileChooser(); InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
public static final String DEFAULT_HOST = "DLKD122-PC7";
public static final int DEFAULT_PORT = 8901;
static String host;
static int port;
static String message;
static TextField hostDisplay, portDisplay;
static TextArea logDisplay, msgDisplay;
Panel topPanel;
Panel middlePanel;
Panel buttonPanel;
Button sendButton, sendFileButton,quitButton; public Client ()
{ super ( "Client" );
buildUI (); } // end Client
public void connectClient ( )
{
try
{
InetAddress here = InetAddress.getLocalHost ();
logDisplay.appendText ( "here = "+here+"\n" );
host = here.getHostName ();
logDisplay.appendText ( "host = "+host+"\n" );
}
catch (UnknownHostException e) { ;}
//host = hostDisplay.getText ();
//if ( host.equals ("" ) )
host = DEFAULT_HOST;
if ( ! ( portDisplay.getText () ).equals ( "" ) )
port = Integer.parseInt ( portDisplay.getText () );
else
port = DEFAULT_PORT;
try
{
connection = new Socket ( InetAddress.getByName (host), port );
//connection = new Socket ( InetAddress.getByAddress (host), port );
outStream = connection.getOutputStream ();
outDataStream = new DataOutputStream ( outStream );
inStream = connection.getInputStream ();
inDataStream = new DataInputStream ( inStream );
logDisplay.setText ( "Socket created: connecting to server\n" );
message = msgDisplay.getText ();
outDataStream.writeUTF ( message );
logDisplay.appendText ( "Message, below, sent to Server\n" );
try
{
msgDisplay.setText ( "" );
msgDisplay.setForeground ( Color.red );
while ( true )
{
message = inDataStream.readUTF ();
msgDisplay.appendText ( message );
logDisplay.appendText ( "Message, below, received from server\n" );
} // end while
} // end try for input
catch ( EOFException except )
{
connection.close ();
logDisplay.appendText ( "Connection closed\n" );
} // end catch
catch ( IOException except )
{
System.out.println ( "IO Exception");
except.printStackTrace ();
} // end catch
} // end try
catch ( IOException except)
{
except.printStackTrace ();
} // end catch
} // end connectClient
public static boolean send( String filename )
{
try
{
InetAddress here = InetAddress.getLocalHost ();
logDisplay.appendText ( "here = "+here+"\n" );
host = here.getHostName ();
logDisplay.appendText ( "host = "+host+"\n" );
}
catch (UnknownHostException e) { ;}
//host = hostDisplay.getText ();
//if ( host.equals ("" ) )
host = DEFAULT_HOST;
if ( ! ( portDisplay.getText () ).equals ( "" ) )
port = Integer.parseInt ( portDisplay.getText () );
else port = DEFAULT_PORT;
try
{
logDisplay.append("Sending data...\n");
Socket skt = new Socket(host, port);
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );
//System.out.println("after new inputstream");
long currTime = System.currentTimeMillis();
//Read, and write the file to the socket
int i;
while ((i = in.read()) != -1)
{
out.write(i);
//System.out.println(i);
}
//out.write(-1); // to signal the end of file??
//Close the socket and the file
//System.out.println("about to read");
//logDisplay.append(message);
out.flush();
out.close();
in.close();
//in1.close();
//in2.close();
skt.close();
//logDisplay.append(""+(System.currentTimeMillis()-currTime));
return true;
}
catch( Exception e )
{
logDisplay.append("Error! It didn't work! " + e + "\n");
return false;
}
}
public static void main ( String [ ] args )
{
Client client = new Client ();
client.addWindowListener ( client );
} // end main public void buildUI ()
{
try
{
InetAddress here = InetAddress.getLocalHost ();
host = here.getHostName ();
}
catch (UnknownHostException e)
{ ;}
hostDisplay = new TextField ( host, 30 );
portDisplay = new TextField ( Integer.toString ( DEFAULT_PORT ), 4 );
topPanel = new Panel ();
topPanel.setLayout ( new GridLayout ( 2, 1 ) );
topPanel.add ( hostDisplay );
topPanel.add ( portDisplay );
logDisplay = new TextArea ( 40, 10 );
msgDisplay = new TextArea ( 40, 10 );
msgDisplay.setText ( "Default Message." );
middlePanel = new Panel ();
middlePanel.setLayout ( new GridLayout ( 2, 1 ) );
middlePanel.add ( logDisplay );
middlePanel.add ( msgDisplay );
sendButton = new Button ( "Send Message" );
sendFileButton = new Button ( "Send File" );
quitButton = new Button ( "Quit" );
sendButton.addActionListener ( this );
sendFileButton.addActionListener ( this );
quitButton.addActionListener ( this );
buttonPanel = new Panel ( );
buttonPanel.add ( sendFileButton );
buttonPanel.add ( sendButton );
buttonPanel.add ( quitButton );
add ( "North", topPanel );
add ( "Center", middlePanel );
add ( "South", buttonPanel );
resize ( 400, 450 );
show ();
} // end buildUI public void actionPerformed ( ActionEvent e )
{
Object s = e.getSource();
// *** process Button actions
if ( s instanceof Button )
{
if ( s == sendButton )
{
connectClient ();
} // end sendButton
if ( s == sendFileButton )
{
int returnVal = fc.showOpenDialog(Client.this); logDisplay.append("Opening - "); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile(); logDisplay.append(file.getName() + "\n"); //Create a client, give it the chosen filename, and send the data across
if (send(file.getPath().toString()))
logDisplay.append("Success." + "\n");
else
logDisplay.append("Failure." + "\n");
} } // end sendButton
if ( s == quitButton )
{
this.hide ();
this.dispose ();
System.exit ( 0 );
} // end quitButton
} // end process Button actions } // end actionPerformed
//**** WindowListener methods
public void windowActivated ( WindowEvent e )
{
}
public void windowDeactivated ( WindowEvent e )
{
}
public void windowOpened ( WindowEvent e )
{
}
public void windowClosed ( WindowEvent e )
{
}
public void windowClosing ( WindowEvent e )
{
hide ();
dispose ();
System.exit(0);
}
public void windowIconified ( WindowEvent e )
{
}
public void windowDeiconified ( WindowEvent e )
{
}
} // end Client