Java Sockets - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Java Sockets

ABDUL HAYEEABDUL HAYEE Posts: 81Member
Dear Sir ,
I am from Virtual University , Pakistan , studying at home . I am stuck with file transfer problem . In text book code , they have used " localhost" as the server . 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 string like "Hello Abdul ... ..." , does pass on and is echoed back.

Please tell me how I can over came the problem ? Please send the code (snippet only) 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 ,
[email protected]
[email protected]

Comments

  • ColacXColacX Posts: 141Member
    Rather show us your code so we can point out whats wrong instead.
  • sstekessteke Posts: 3Member
    If you are using Windows XP, Vista , disable firewall on both computers . and show us your code first.

  • ABDUL HAYEEABDUL HAYEE Posts: 81Member
    : Rather show us your code so we can point out whats wrong instead.
    ....................
    Dear Sir ,
    The code is as under :
    Regards,
    ah.



    /* "File transfer via sockets". */

    /* i make a client - server file transfer but when server receive file i want serve send a msg to client that take the file. I try different but nothing.

    my code is:

    */

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

    import java.net.*;
    import java.io.*;

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

    DataInputStream input;

    BufferedInputStream bis;
    BufferedOutputStream bos;
    int in;
    byte[] byteArray;

    try {

    Socket client = new Socket("192.168.0.33", 2222);

    input = new DataInputStream (client.getInputStream() );

    output.writeUTF( "send a file" );
    System.out.println("Server message: " +input.readUTF() );

    bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt"));
    bos = new BufferedOutputStream(client.getOutputStream());
    byteArray = new byte[9999]; // Looks enough for a small file
    while ((in = bis.read(byteArray)) != -1){
    bos.write(byteArray,0,in);
    }
    bis.close();
    bos.close();

    System.out.println("Server message: " +input.readUTF() );
    }
    catch ( Exception e ) {
    System.err.println(e);
    }
    }
    }

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

    import java.net.*;
    import java.io.*;

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

    ServerSocket server;
    Socket connection;

    DataOutputStream output;

    BufferedInputStream bis;
    BufferedOutputStream bos;

    byte[] receivedData;
    int in;
    try {
    server = new ServerSocket( 2222 );

    while ( true ) {
    connection = server.accept();

    output = new DataOutputStream (connection.getOutputStream() );

    System.out.println( "Client message: " +input.readUTF() );
    output.writeUTF( " ack 1" );

    receivedData = new byte[9999];
    bis = new BufferedInputStream(connection.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream("stars.txt"));
    while ((in = bis.read(receivedData)) != -1){
    bos.write(receivedData,0,in);
    }
    bos.close();

    output.writeUTF( " ack 2" );
    }
    }
    catch (IOException e ) { }
    }
    }

    /* ********** File : stars.txt ************ */

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



    /* The error shown is:
    java.net.SocketException: socket closed


    */
  • ABDUL HAYEEABDUL HAYEE Posts: 81Member
    : If you are using Windows XP, Vista , disable firewall on both
    : computers . and show us your code first.
    :
    :
    ........................................................................
    Dear Sir ,
    I am using windows XP . with built in firewall . There is no additional firewall . My codes are as under :





    /* "File transfer via sockets". */

    /* i make a client - server file transfer but when server receive file i want serve

    send a msg to client that take the file. I try different but nothing.

    my code is:

    */

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

    import java.net.*;
    import java.io.*;

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

    DataInputStream input;

    BufferedInputStream bis;
    BufferedOutputStream bos;
    int in;
    byte[] byteArray;

    try {

    Socket client = new Socket("192.168.0.33", 2222);

    input = new DataInputStream (client.getInputStream() );

    output.writeUTF( "send a file" );
    System.out.println("Server message: " +input.readUTF() );

    bis = new BufferedInputStream(new FileInputStream("encryptAtmMsg.txt"));
    bos = new BufferedOutputStream(client.getOutputStream());
    byteArray = new byte[9999]; // Looks enough for a small file
    while ((in = bis.read(byteArray)) != -1){
    bos.write(byteArray,0,in);
    }
    bis.close();
    bos.close();

    System.out.println("Server message: " +input.readUTF() );
    }
    catch ( Exception e ) {
    System.err.println(e);
    }
    }
    }

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

    import java.net.*;
    import java.io.*;

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

    ServerSocket server;
    Socket connection;

    DataOutputStream output;

    BufferedInputStream bis;
    BufferedOutputStream bos;

    byte[] receivedData;
    int in;
    try {
    server = new ServerSocket( 2222 );

    while ( true ) {
    connection = server.accept();

    output = new DataOutputStream (connection.getOutputStream() );

    System.out.println( "Client message: " +input.readUTF() );
    output.writeUTF( " ack 1" );

    receivedData = new byte[9999];
    bis = new BufferedInputStream(connection.getInputStream());
    bos = new BufferedOutputStream(new FileOutputStream("stars.txt"));
    while ((in = bis.read(receivedData)) != -1){
    bos.write(receivedData,0,in);
    }
    bos.close();

    output.writeUTF( " ack 2" );
    }
    }
    catch (IOException e ) { }
    }
    }

    /* ********** File : stars.txt ************ */

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



    /* The error shown is:
    java.net.SocketException: socket closed


    */
  • sstekessteke Posts: 3Member
    Refer the book, Java Network Programming by Elloit rusty Harold, Orielly publication.

    Examples from this books can be found at http://www.cafeaulait.org/books/jnp/javanetexamples/

    I am pasting a good example from this book which have good server implementation
    ----------------------------------------

    import java.net.*;
    import java.io.*;

    public class clientTester {

    public static void main(String[] args) {

    int thePort;
    ServerSocket ss;
    Socket theConnection;

    try {
    thePort = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
    thePort = 0;
    }

    try {
    ss = new ServerSocket(thePort);
    System.out.println("Listening for connections on port " + ss.getLocalPort());

    while (true) {
    theConnection = ss.accept();
    System.out.println("Connection established with " + theConnection);
    InputThread it = new InputThread(theConnection.getInputStream());
    it.start();
    OutputThread ot = new OutputThread(theConnection.getOutputStream(), it);
    ot.start();
    // need to wait for ot and it to finish
    try {
    ot.join();
    it.join();
    }
    catch (InterruptedException e) {
    }
    }
    }
    catch (IOException e) {

    }

    }

    }

    class InputThread extends Thread {

    InputStream is;

    public InputThread(InputStream is) {
    this.is = is;
    }

    public void run() {

    try {
    while (true) {
    int i = is.read();
    if (i == -1) break;
    char c = (char) i;
    System.out.print(c);
    }
    }
    catch (IOException e) {
    System.err.println(e);
    }

    }

    }

    class OutputThread extends Thread {

    PrintStream ps;
    DataInputStream is;
    InputThread it;

    public OutputThread(OutputStream os, InputThread it) {
    ps = new PrintStream(os);
    this.it = it;
    is = new DataInputStream(System.in);
    }

    public void run() {

    String line;
    try {
    while (true) {
    line = is.readLine();
    if (line.equals(".")) break;
    ps.println(line);
    }
    }
    catch (IOException e) {

    }
    it.stop();
    }

    }
Sign In or Register to comment.