steps
Posts: 14
Nickname: spanishste
Registered: Nov, 2003
|
|
Re: Create a directory on the server
|
Posted: Feb 13, 2004 10:56 AM
|
|
Adam,
Here's what am trying to do:I've a GUI where the user enters his username and password.After, the user is authenticated, he shld be able to send files to a remote pc.Ip-address and port no's would be the inputs.when the user enters the ip-address and port no and press a button, a directory has to be created on the remote pc and then a file chooser program will open in order to send files.I dont want to use the sun.net.ftp package.I've written a simple client/server program.With this program, I'm able to send the file to a remote pc but the contents are written into a txt file. I'm quiet confused with the streams.Could somebody please help me with my code.
Thanks in advance
Client code
public void tcpClient() {
int port = 1234;
String server = "xxx.xxx.xxx.xx";//Remote pc Ip-address
Socket socket = null;
int ERROR = 1;
//connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server "+socket.getInetAddress()+":"+socket.getPort());
}
catch (UnknownHostException e) {
errortext.setText("Could not connect with server");
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}
try {
byte[] buf = new byte[1024];
OutputStream os = socket.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(os,1024);
String file = uploadfile; // the file being read.
FileInputStream in = new FileInputStream(file);
int i;
int bytecount=1024;
while ((i = in.read(buf,0,1024)) != -1) {
bytecount=bytecount+1024;
out.write(buf,0,1024);
out.flush();
}
in.close();
out.close();
System.out.println("Bytes Sent :"+bytecount);
errortext.setText("Sending File");
}
catch (IOException e) {
System.out.println(e);
}
try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
Server code
import java.net.*;
import java.io.*;
public class tcpServer {
public static MatlabRCServer serverrc;
public MyField myf;
//public String uploadfile;
//private javax.swing.JTextField errortext;
public static void main(String args[]) {
serverrc = new MatlabRCServer();
int port;
ServerSocket server_socket;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1234 (default)");
port = 1234;
}
try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " + server_socket.getLocalPort());
// server infinite loop
while(true) {
Socket socket = server_socket.accept();
System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
try {
byte[] b = new byte[1024];
int len=0;
int bytcount=1024;
String FileName="sent.txt"; //writing to a file named ftptest.txt
FileOutputStream inFile = new FileOutputStream(FileName);
InputStream is = socket.getInputStream();
BufferedInputStream in2 = new BufferedInputStream(is,1024);
while( (len = in2.read(b,0,1024)) != -1 ) {
bytcount=bytcount+1024;
inFile.write(b,0,1024);
}
in2.close();
inFile.close();
System.out.println("Bytes Writen : "+bytcount);
//errortext.setText("Upload Successful");
}
catch(IOException e) {
System.out.println("Unable to open file" + e);
return;
}
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
|
|