This week's book giveaway is in the
General Computing
forum.
We're giving away four copies of
Arduino in Action
and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Sockets and Internet Protocols
Author
Null Pointer Exception with SocketChannel
glup klosar
Greenhorn
Joined: May 04, 2009
Posts: 18
posted
May 05, 2009 01:22:14
0
Here is the code, any help/ideas/suggestions are greatly appreciated :
package DataTransfer; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; class Send extends Thread{ String receiverName; String senderName; String outputFileName; String inputFileName; public Send(String sn, String rn, String inf, String of){ senderName = sn; receiverName = rn; inputFileName = inf; outputFileName = of; } public void run(){ try{ InetAddress receiverAddress = InetAddress.getByName(receiverName); int port=4444, localPort=4444; InetAddress localAddr = InetAddress.getByName(senderName); System.out.println("start send"); Socket sendSocket = new Socket(receiverAddress, port, localAddr, localPort); OutputStream dataOutput = sendSocket.getOutputStream(); FileInputStream in = new FileInputStream(inputFileName); FileChannel fromFile = in.getChannel(); SocketChannel toSocket; SocketChannel.open(); //this is apparently the right order toSocket = sendSocket.getChannel(); System.out.println(toSocket==null);//prints true System.out.println(toSocket.isConnected());//throws NullPointerException long bytesWritten = 0; long byteCount = fromFile.size(); while (bytesWritten < byteCount) bytesWritten += fromFile.transferTo(bytesWritten, byteCount - bytesWritten, toSocket); //problem sendSocket.close(); fromFile.close(); toSocket.close(); dataOutput.close();in.close(); }catch (IOException e) {System.out.println(e.toString());} } }
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8290
I like...
posted
May 05, 2009 06:07:50
0
Please do not post the
same question
more than once.
CarefullyChooseOneForum
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[
How To Ask Questions On JavaRanch
]
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: Null Pointer Exception with SocketChannel
Similar Threads
ServerSocketChannel.accept() seems to create a random port to send data to?
Socket/TCPIP help required
SocketChannel and FileChannel
NIO Socket weirdnes in Solaris 2.10
tranfering big files (over a network)
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter