简单发消息
public class TCPTest {
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet1 = InetAddress.getByName("10.44.98.151");
socket = new Socket(inet1, 1111);
os = socket.getOutputStream();
os.write("hello,i'm client".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void sever() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
ss = new ServerSocket(1111);
socket = ss.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] va = new byte[20];
int len;
while((len = is.read(va)) != -1) {
baos.write(va);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
操作文件
public class TCPTest1 {
@Test
public void employee() {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fis = null;
try {
File file = new File("0010.jpeg");
socket = new Socket("10.44.98.151", 1111);
outputStream = socket.getOutputStream();
fis = new FileInputStream("0010.jpeg");
int len;
byte[] bys = new byte[1024];
while ((len = fis.read(bys)) != -1) {
outputStream.write(bys, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void manager() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fos = null;
try {
serverSocket = new ServerSocket(1111);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
fos = new FileOutputStream("002.jpeg");
byte[] bys = new byte[1024];
int len;
while ((len =inputStream.read(bys)) != -1) {
fos.write(bys,0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
收到后回复
public class TCPTest2 {
@Test
public void user() {
Socket socket = null;
InputStream inputStream =null;
OutputStream outputStream = null;
try {
socket = new Socket("10.44.98.151", 1111);
outputStream = socket.getOutputStream();
outputStream.write("i need your serve".getBytes(StandardCharsets.UTF_8));
socket.shutdownOutput();
inputStream = socket.getInputStream();
ByteArrayOutputStream bys = new ByteArrayOutputStream();
byte[] by = new byte[20];
int len;
while ((len = inputStream.read(by)) !=-1) {
bys.write(by,0,len);
}
System.out.println(bys);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(1111);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
ByteArrayOutputStream bys = new ByteArrayOutputStream();
byte[] by = new byte[20];
int len;
while ((len = inputStream.read(by)) != -1) {
bys.write(by, 0, len);
}
System.out.println(bys);
outputStream = socket.getOutputStream();
outputStream.write("i hava receeved".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}