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
Forums
Register
Login
Java
»
Sockets and Internet Protocols
Null Pointer Exception with SocketChannel
glup klosar
,
Greenhorn
May 05, 2009 01:22:14
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
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
staff
May 05, 2009 06:07:50
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Please do not post the
same question
more than once.
CarefullyChooseOneForum
Post by:
autobot
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
This thread has been viewed 2412 times.
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...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:13:54
.