Please help me, I have a server.java and client.java to test the file transmission on internet
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:
//server
import 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");
}
}
}