Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Sockets and Internet Protocols
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Sockets and Internet Protocols
Null Pointer Exception with SocketChannel
glup klosar
Greenhorn
Posts: 18
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 9626
16
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please do not post the
same question
more than once.
CarefullyChooseOneForum
[
How To Ask Questions On JavaRanch
]
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
SocketChannel and FileChannel
NIO Socket weirdnes in Solaris 2.10
Socket/TCPIP help required
ServerSocketChannel.accept() seems to create a random port to send data to?
Connection timed out: Could be due to invalid connection
More...