Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Java Sockets Posted by ABDUL HAYEE on 28 Jul 2008 at 4:36 AM
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 ,
ahayee84@hotmail.com
ahayee84@yahoo.com
Report
Re: Java Sockets Posted by ColacX on 28 Jul 2008 at 6:42 AM
Rather show us your code so we can point out whats wrong instead.
Report
Re: Java Sockets Posted by ABDUL HAYEE on 29 Jul 2008 at 10:55 PM
: 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


*/
Report
Re: Java Sockets Posted by ssteke on 28 Jul 2008 at 12:56 PM
If you are using Windows XP, Vista , disable firewall on both computers . and show us your code first.

Report
Re: Java Sockets Posted by ABDUL HAYEE on 29 Jul 2008 at 11:02 PM
: 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


*/
Report
Re: Java Sockets Posted by ssteke on 30 Jul 2008 at 12:21 PM
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();
}

}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.