Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket. The Socket will also bind to the local address and port supplied. Creates a socket and connects it to the specified port number at the specified IP address.
Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs. Binds the socket to a local address. Returns the address to which the socket is connected. Gets the local address to which the socket is bound. Returns the remote port number to which this socket is connected.
Returns the local port number to which this socket is bound. Returns the address of the endpoint this socket is bound to. Returns an input stream for this socket. Returns an output stream for this socket. The maximum timeout value is platform specific. The setting only affects socket close.
Send one byte of urgent data on the socket. The byte to be sent is the lowest eight bits of the data parameter. The urgent byte is sent after any preceding writes to the socket OutputStream and before any future writes to the OutputStream. If the user wishes to receive urgent data, then this option must be enabled. When enabled, urgent data is received inline with normal data.
With this option set to a non-zero timeout, a read call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java. SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. As the underlying network implementation may ignore this value applications should consider it a hint.
Notes: For Internet Protocol v4 the value consists of an integer , the least significant 8 bits of which represent the value of the TOS octet in IP packets sent by the socket.
Gets traffic class or type-of-service in the IP header for packets sent from this Socket As the underlying network implementation may ignore the traffic class or type-of-service set using setTrafficClass int this method may return a different value than was previously set using the setTrafficClass int method on this Socket.
Closes this socket. If this socket has an associated channel then the channel is closed as well. See Also: setSocketImplFactory java. If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Parameters: address - the IP address. NullPointerException - if address is null. String, int Socket public Socket String host, int port, InetAddress localAddr, int localPort throws IOException Creates a socket and connects it to the specified remote host on the specified remote port.
The Socket will also bind to the local address and port supplied. A local port number of zero will let the system pick up a free port in the bind operation. Parameters: host - the name of the remote host, or null for the loopback address. IllegalArgumentException - if the port parameter or localPort parameter is outside the specified range of valid port values, which is between 0 and , inclusive.
If the specified local address is null it is the equivalent of specifying the address as the AnyLocal address see InetAddress. Parameters: address - the remote address port - the remote port localAddr - the local address the socket is bound to, or null for the anyLocal address. Creates a stream socket and connects it to the specified port number on the named host.
If the stream argument is true , this creates a stream socket. If the stream argument is false , it creates a datagram socket. Creates a socket and connects it to the specified port number at the specified IP address. If there is a security manager, its checkConnect method is called with host. Parameters: host - the IP address. NullPointerException - if host is null. Parameters: endpoint - the SocketAddress Throws: IOException - if an error occurs during the connection IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket Since: 1.
A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs. Parameters: endpoint - the SocketAddress timeout - the timeout value to be used in milliseconds. Throws: IOException - if an error occurs during the connection SocketTimeoutException - if timeout expires before connecting IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket Since: 1.
If the address is null , then the system will pick up an ephemeral port and a valid local address to bind the socket. Parameters: bindpoint - the SocketAddress to bind to Throws: IOException - if the bind operation fails, or if the socket is already bound. IllegalArgumentException - if bindpoint is a SocketAddress subclass not supported by this socket Since: 1.
If the socket was connected prior to being closed , then this method will continue to return the connected address after the socket is closed. Returns: the remote IP address to which this socket is connected, or null if the socket is not connected. Returns: the local address to which the socket is bound, or the wildcard address if the socket is closed or not bound yet.
If the socket was connected prior to being closed , then this method will continue to return the connected port number after the socket is closed. Returns: the remote port number to which this socket is connected, or 0 if the socket is not connected yet.
If the socket was bound prior to being closed , then this method will continue to return the local port number after the socket is closed. Returns: the local port number to which this socket is bound or -1 if the socket is not bound yet. Returns: a SocketAddress representing the remote endpoint of this socket, or null if it is not connected yet. If a socket bound to an endpoint represented by an InetSocketAddress is closed , then this method will continue to return an InetSocketAddress after the socket is closed.
In that case the returned InetSocketAddress 's address is the wildcard address and its port is the local port that it was bound to. Returns: a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet. A socket will have a channel if, and only if, the channel itself was created via the SocketChannel. Returns: the socket channel associated with this socket, or null if this socket was not created for a channel Since: 1. If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel.
If the channel is in non-blocking mode then the input stream's read operations will throw an IllegalBlockingModeException. Under abnormal conditions the underlying connection may be broken by the remote host or the network software for example a connection reset in the case of TCP connections. When a broken connection is detected by the network software the following applies to the returned input stream :- The network software may discard bytes that are buffered by the socket.
Bytes that aren't discarded by the network software can be read using read. If there are no bytes buffered on the socket, or all buffered bytes have been consumed by read , then all subsequent calls to read will throw an IOException.
If there are no bytes buffered on the socket, and the socket has not been closed using close , then available will return 0. Closing the returned InputStream will close the associated socket. I have dealt with this before and ObjectInputStreams sometimes do not behave correctly. In my case I have found that the server connection needs to create a ObjectOutputStream first otherwise it will return null like its doing.
Is the server code your own, if so make sure you create the ObjectOutputStream first before trying to create ObjectInputStream on client.
Here is a link that helps describe the issue I had a while back that is similiar to what your question is. It has to do with the order or squence of streams being created. Object outputstream programmig. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why does my sockets getInputStream return null? Ask Question. Asked 9 years, 1 month ago.
Active 9 years, 1 month ago. Input Stream: [email protected]. Exception in thread "main" java. SocketException: Socket is closed at java. SocketException: Socket is not connected at java. Next Topic Java Socket Class. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle.
0コメント