The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Sockets

3 replies on 1 page. Most recent reply: Jul 31, 2008 1:08 AM by Abdul Hayee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Abdul Hayee

Posts: 4
Nickname: ahayee84
Registered: Jul, 2008

Java Sockets Posted: Jul 28, 2008 5:35 AM
Reply to this message Reply
Advertisement
Hello !
I am from Virtual University , Pakistan . I am stuck with file transfer problem using sockets . In my code , I have used " localhost" as the server . Now I am trying to send a text file from my computer IP 192.168.1.33 to another computer next door ( on the same LAN ) IP 192.168.1.34 . Same is not getting through , presumably the coding problem. However , simple strings like "Hello Abdul ...... " , do pass on and are echoed back.

Please tell me how I can over came the problem ? Please send the code to send a text file like " stars.txt " , then recieving the confirmation of transfer. Same may be displayed on message box , or any other form.

Please help me as I am badly stuck , despite of trying several different codes. Thanking you with sincere wishes,

Abdul hayee ,
ahayee84@hotmail.com
ahayee84@yahoo.com


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java Sockets Posted: Jul 29, 2008 12:38 AM
Reply to this message Reply
Not quite sure how much you have written. There is an apache has an FTP library if you are doing a file transfer.
Don't quite remember if it is in Apache commons.

You simply place the Jar in your Class Path and you can do stuff like:

FTPClient myFtpClient = new FTPClient();
ftpClient.connect("someFTPServer.com", "user", "password");
 
//  Use things like sendCommand to send FTP commands
//  Better yet just Google:  Apache Java FTP Upload for examples
 


Naturally you should have an FTP Server running and configured appropriately on the second machine.

If this is not what you are looking for, you will need to write your own Server (which would work similar to an FTP Server) you can probably Google Java FTP Server OpenSource to look at Code examples. This will reside on the machine you want to transfer the file to - (will behave the same way an ftp server responds to put). You will also need to write a Client.

Good luck.

Abdul Hayee

Posts: 4
Nickname: ahayee84
Registered: Jul, 2008

Re: Java Sockets Posted: Jul 30, 2008 8:45 AM
Reply to this message Reply
> Hello !
> I am from Virtual University , Pakistan . I am
> n . I am stuck with file transfer problem using sockets
> . In my code , I have used " localhost" as the server .
> Now I am trying to send a text file from my computer IP
> 192.168.1.33 to another computer next door ( on the same
> e LAN ) IP 192.168.1.34 . Same is not getting through ,
> presumably the coding problem. However , simple strings
> like "Hello Abdul ...... " , do pass on and are echoed
> back.
>
> Please tell me how I can over came the
> can over came the problem ? Please send the code to send
> a text file like " stars.txt " , then recieving the
> confirmation of transfer. Same may be displayed on message
> box , or any other form.
>
> Please help me as I am badly stuck ,
> I am badly stuck , despite of trying several different
> codes. Thanking you with sincere wishes,
>
> Abdul hayee ,
> ahayee84@hotmail.com
> ahayee84@yahoo.com

Hello Spike ,
Thanks for reply .
I will try tonight and then send you feed back , if there is some bottleneck. Regards,
ahsyee84

Abdul Hayee

Posts: 4
Nickname: ahayee84
Registered: Jul, 2008

Re: Java Sockets Posted: Jul 31, 2008 1:08 AM
Reply to this message Reply
/* The program bellow works well , with my name " Abdul "
and returns " Hellow Abdul "
But does not take the accept the file " stars.txt "
which cannot be transfred.

I could not manage to induct , the suggestions you provided
Plz indicate what changes are necessary ?

*/


/* ********** Client ************ */

import java.net.*;
import java.io.*;
import javax.swing.*;

public class EchoClient{
public static void main(String args[]){
try {

Socket s = new Socket("localhost", 2222);

InputStream is = s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new
PrintWriter(os,true);

// Send / Receive message
// asking user to enter his/her name

String msg = JOptionPane.showInputDialog("Enter your name");

// sending name to server
pw.println(msg);

// reading message (name appended with hello) from
// server
msg = br.readLine();

// displaying received message
JOptionPane.showMessageDialog(null , msg);

// closing communication socket
s.close();

}
catch(Exception ex){
System.out.println(ex);
}
}
} /



/* ************* Server ********************** */



import java.net.*;
import java.io.*;
import javax.swing.*;

public class EchoServer
{
public static void main(String args[])
{
try
{
// create a server socket
ServerSocket ss = new ServerSocket(2222);
System.out.println("Server started...");

/* Loop back to the accept method of the serversocket and wait for a
* new connection request. So server will continuously listen for requests */
while(true) {
// wait for incoming connection
Socket s = ss.accept();
System.out.println("connection request recieved");

// Get I/O streams
InputStream is = s.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os,true);

// Send / Receive message
// reading name sent by client
String name = br.readLine();

// appending “hello” with the received name

String msg = "Hello " + name + " from Server";

// sending back to client
pw.println(msg);

// closing communication sockeys.close();
} // end while
}

catch(Exception ex){
System.out.println(ex);
}
}
} // end class



/* *********** stars.txt ********** */

Good friends are like stars........
You don't always see them,
but you know they are always there.







/*

Flat View: This topic has 3 replies on 1 page
Topic: DevWeek India 2008- No Marketing Bushwah, Only Real Information Previous Topic   Next Topic Topic: How to get classes present in the imported package

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use