Java Networking: Socket

Java Networking

1 Java Networking
2 Java Networking: Socket
3 Java Networking: ServerSocket
4 Java Networking: UDP DatagramSocket
5 Java Networking: URL + URLConnection
6 Java Networking: JarURLConnection
7 Java Networking: InetAddress
8 Java Networking: Protocol Design

Java Networking: Socket

 
By Jakob Jenkov

 Connect with me:

Rate article:

Share article:

Tweet

In order to connect to a server over the internet (via TCP/IP) in Java, you need to create a java.net.Socket and connect it to the server. Alternatively you can use a Java NIO SocketChannel, in case you prefer to use Java NIO.

Creating a Socket

This code example connects to the server with IP address 78.46.84.171 on port 80. That server happens to be my web server (www.jenkov.com), and port 80 is the web servers port.

Socket socket = new Socket("78.46.84.171", 80);

You can also use a domain name instead of an IP address, like this:

Socket socket = new Socket("jenkov.com", 80);

Writing to a Socket

To write to a Java Socket you must obtain its OutputStream. Here is how that is done:

Socket socket = new Socket("jenkov.com", 80);
OutputStream out = socket.getOutputStream();

out.write("some data".getBytes());
out.flush();
out.close();

socket.close();

That‘s how simple it is!

Don‘t forget to call flush() when you really, really want the data sent across the internet to the server. The underlying TCP/IP implementation in your OS may buffer the data and send it in larger chunks to fit with with the size of TCP/IP packets.

Reading from a Socket

To read from a Java Socket you will need to obtains its InputStream. Here is how that is done:

Socket socket = new Socket("jenkov.com", 80);
InputStream in = socket.getInputStream();

int data = in.read();
//... read more data...

in.close();
socket.close();

Pretty simple, right?

Keep in mind that you cannot always just read from the Socket‘s InputStream until it returns -1, as you can when reading a file. The reason is that -1 is only returned when the server closes the connection. But a server may not always close the connection. Perhaps you want to send multiple requests over the same connection. In that case it would be pretty stupid to close the connection.

Instead you must know how many bytes to read from the Socket‘s InputStream. This can be done by either the server telling how many bytes it is sending, or by looking for a special end-of-data character.

Closing a Socket

When you are done using a Java Socket you must close it to close the connection to the server. This is done by calling the Socket.close() method, like this:

Socket socket = new Socket("jenkov.com", 80);

socket.close();

时间: 2024-10-24 11:25:47

Java Networking: Socket的相关文章

Java Networking

1 Java Networking 2 Java Networking: Socket 3 Java Networking: ServerSocket 4 Java Networking: UDP DatagramSocket 5 Java Networking: URL + URLConnection 6 Java Networking: JarURLConnection 7 Java Networking: InetAddress 8 Java Networking: Protocol De

Java Secure Socket Extension (JSSE) Reference Guide

Skip to Content Oracle Technology Network Software Downloads Documentation Search Java Secure Socket Extension (JSSE) Reference Guide This guide covers the following topics: Skip Navigation Links Introduction Features and Benefits JSSE Standard API S

Java基于Socket文件传输示例(转)

最近需要进行网络传输大文件,于是对基于socket的文件传输作了一个初步的了解.在一位网友提供的程序基础上,俺进行了一些加工,采用了缓冲输入/输出流来包装输出流,再采用数据输入/输出输出流进行包装,加快传输的速度.废话少说,先来看服务器端的程序. 1.服务器端 package sterning; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream;

java.net.ServerSocket和java.net.Socket

个人博客地址:http://www.cnblogs.com/wdfwolf3/ java.net.ServerSocket 1.构造函数 a.ServerSocket() 创建一个无连接的server socket. b.ServerSocket(int port) 绑定到port端口上 c.ServerSocket(int port, int backlog) backlog表示等待连接的队列最大长度 d.ServerSocket(int port, int backlog, InetAddr

java网络socket编程详解

7.2 面向套接字编程    我们已经通过了解Socket的接口,知其所以然,下面我们就将通过具体的案例,来熟悉Socket的具体工作方式 7.2.1使用套接字实现基于TCP协议的服务器和客户机程序    依据TCP协议,在C/S架构的通讯过程中,客户端和服务器的Socket动作如下: 客户端: 1.用服务器的IP地址和端口号实例化Socket对象. 2.调用connect方法,连接到服务器上. 3.将发送到服务器的IO流填充到IO对象里,比如BufferedReader/PrintWriter

Java通过socket实现smtp协议发送邮件

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;import java.net.UnknownHostException; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; /** 

java,Socket,NIO随笔记录

这个答案答的很好.Socket与SocketChannel是俩套api而已,对于网络tcp通信而言不会关心你上层是用何api实现通信的.所以答案是肯定的.SocketChannel可以设置为非阻塞的,所以在某种情况下性能更好,线程不会被挂住.SocketChannel还能注册selector和感兴趣的事件.selector多路复用器这里就不多做介绍了. 在这里介绍一下通信过程中数据的流动过程.首先当数据被网卡接收后,会被保存到内核中,电脑进行拆包解析,看源端口号,会将此数据包保存到对应链接的tc

java下socket传图片

package cn.stat.p4.ipdemo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class imageserver { /**

基于java的socket编程

#开头的废话#学习java已经半个月了,原本在抠教材里面的字眼时,觉得教材好厚,要看完不知道要到猴年马月去了.突然在网上看到一个教程,里面老师说学编程语言书不用太细看,看个大概,知道里面讲些什么就好,不用全记得,然后你一个劲地编,使劲地编,编的时候不懂再回来看就好了,这是最快的方法.心里一琢磨,还真是这样,根据以前学C语言的情况不就这样吗.所以便加速看,把一些书里介绍的方法,类飞速地浏览过了,刷到网络这一章,觉得socket编程应该是得试一下手,不要只看不做假把式. 此文为原创,转摘请注明转摘自