TCPClient
package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
public class TCPClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.0.XXXX", 8066);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server!123456");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println("服务端输出:"+dis.readUTF());
dos.flush();
dos.close();
s.close();
}
}
TCPServer
package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
public class TCPServer {
public static void main(String[] args){
ServerSocket ss;
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
ss = new ServerSocket(8066);
s = ss.accept();
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
String data = AndroidDataUtil.ProgramListContext();
boolean flag = true;
while (flag) {
System.out.println("服务端接收到客户端输出信息:"+dis.readUTF());
dos.writeUTF(data);
System.out.println("服务端返回给客户端的数据:");
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
TCPServer-接受客户端发送数据,返回给客户端数据
package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class TCPServer {
public static void main(String[] args) {
ServerSocket ss;
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
ss = new ServerSocket(8067);
s = ss.accept();
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
while (true) {
System.out.println("服务端输出:"+dis.readUTF());
dos.writeUTF("{'test':'123456567'}");
System.out.println("返回给客户端的数据:");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
TCPClient
package com.styl.frame.core.controller.newsDelivery.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.*;
public class TCPClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.0.xxxxx", 8066);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server!???????123456");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println("服务端输出:"+dis.readUTF());
dos.flush();
dos.close();
s.close();
}
}