import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
private final static Logger logger = LoggerFactory.getLogger(SocketServer.class.getName());
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("IP地址", 端口);
OutputStream netOut = socket.getOutputStream();
DataOutputStream doc = new DataOutputStream(netOut);
DataInputStream in = new DataInputStream(socket.getInputStream());
//向服务器端第一次发送字符串
doc.writeUTF("send1");
String returnV = in.readUTF();
System.out.println(returnV);
//logger.info(returnV);
//向服务器端第二次发送字符串
doc.writeUTF("send2");
returnV = in.readUTF();
System.out.println(returnV);
//logger.info(returnV);
doc.close();
in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
import org.slf4j.LoggerFactory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
private final static Logger logger = LoggerFactory.getLogger(SocketServer.class.getName());
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("IP地址", 端口);
OutputStream netOut = socket.getOutputStream();
DataOutputStream doc = new DataOutputStream(netOut);
DataInputStream in = new DataInputStream(socket.getInputStream());
//向服务器端第一次发送字符串
doc.writeUTF("send1");
String returnV = in.readUTF();
System.out.println(returnV);
//logger.info(returnV);
//向服务器端第二次发送字符串
doc.writeUTF("send2");
returnV = in.readUTF();
System.out.println(returnV);
//logger.info(returnV);
doc.close();
in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}