Socket socket = new Socket("127.0.0.1",1114);
调用socket对象的getOutputStream获取连接的输入输出流:
Socket socket = new Socket("127.0.0.1",1114);
InputStream inputStream = socket.getInputStream();
dataOutputStream = new DataOutputStream(inputStream);
此时,我们可以通过获取到的输出流向服务器发送信息了,但为了使服务器辨别客户端消息的长度,因此在发送信息前,先发送一个字节表示消息长度:
String message = "你好,服务器!";
byte[] body = message.getBytes();
byte length = (byte) body.length;
//发送消息长度
dataOutputStream.write(length);
//发送消息内容
dataOutputStream.write(body);
dataOutputStream.flush();
那么此时整个客户端向服务器发送消息的功能就完成了,完整的代码就是:
package com.gto.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket socket;
private DataOutputStream dataOutputStream;
public void initConnect(){
try {
socket = new Socket("127.0.0.1",1114);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendMessage(String message){
byte[] body = message.getBytes();
byte length = (byte) body.length;
try {
//发送消息长度
dataOutputStream.write(length);
//发送消息内容
dataOutputStream.write(body);
dataOutputStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = new Client();
client.initConnect();
client.sendMessage("你好,服务器!");
}
}
客户端制作完成后,再做一个简陋的服务端进行接收数据,此时就不能使用Socket类了,java另外提供了一个封装类作为服务端启动tcp连接:ServerSocket,ServerSocket内部提供了一个accept方法,此时,当一个客户端发起了连接请求时,accept方法将返回一个Socket,这个Socket对应着这个客户端发起的连接。
ServerSocket serverSocket = new ServerSocket(1114)
Socket socket = serverSocket.accept();
相应的,可以通过返回的Socket对象,获取一个输入流进行读取客户端发来的数据:
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
//获取消息的长度
int length = dataInputStream.read();
//获取消息
byte[] body = new byte[length];
dataInputStream.read(body);
String message = new String(body);
System.out.println("客户端说:"+message);
此时,服务端接收数据也完成了,完整代码如下:
package com.gto.client;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket serverSocket;
private DataInputStream dataInputStream;
public void startServer(){
try {
serverSocket = new ServerSocket(1114);
Socket socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
GetMessageFromClient();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void GetMessageFromClient(){
try {
//获取消息的长度
int length = dataInputStream.read();
//获取消息
byte[] body = new byte[length];
dataInputStream.read(body);
String message = new String(body);
System.out.println("客户端说:"+message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Server server = new Server();
server.startServer();
}
}
先后运行服务端和客户端代码,就完成了一次客户端发送消息的过程。