<span style="font-size:18px;">package dom4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
-----简单-------方式
import org.junit.Test;
//客户端给服务端发送信息,服务端接收信息将信息打印到控制台上,并给予客户端相应的反馈信息
public class TestTCP1 {
@Test
public void client(){
Socket socket=null;
OutputStream ops=null;
BufferedOutputStream bos=null;
BufferedReader br1=null;
BufferedReader br=null;
BufferedWriter bw=null;
InputStreamReader ips=null;
InputStreamReader ips1=null;
OutputStreamWriter opw=null;
try {
//创建一个Socket对象
socket = new Socket(InetAddress.getByName("127.0.0.1"),9012);
//获取输出流
ops = socket.getOutputStream();
//转换成字符流
opw = new OutputStreamWriter(ops);
//加缓冲流
bw=new BufferedWriter(opw);
//从控制台获取输入流
ips=new InputStreamReader(System.in);
br=new BufferedReader(ips);
char[] c=new char[24];
String str=null;
while((str=br.readLine())!=null){
if(str.equals("bye")){
bw.write("通信终止!");
bw.flush();
socket.shutdownOutput();
break;
}
bw.write(str);
bw.newLine();
bw.flush();
}
System.out.println("发送完毕!");
//从服务器获取回馈信息
ips1=new InputStreamReader(socket.getInputStream());
br1=new BufferedReader(ips1);
String str1=null;
while((str1=br1.readLine())!=null){
System.out.println("收到来自于:"+socket.getInetAddress().getHostName()+"-->"+"的反馈信息!"+str1);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ips!=null){
try {
ips.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ips1!=null){
try {
ips1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(opw!=null){
try {
opw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br1!=null){
try {
br1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ops!=null){
try {
ops.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void Server(){
ServerSocket ss=null;
Socket s=null;
BufferedInputStream bis=null;
BufferedWriter ops=null;
BufferedReader br=null;
InputStreamReader isr=null;
OutputStreamWriter bos=null;
try {
// boolean flag=true;
// while(flag){
ss = new ServerSocket(9012);
s = ss.accept();
isr = new InputStreamReader(s.getInputStream());
br=new BufferedReader(isr);
String str=null;
while((str=br.readLine())!=null){
if(str.equals("bye")){
System.out.println("收到来自于:"+s.getInetAddress().getHostName()+"-->"+str);
break;
}
System.out.println("收到来自于:"+s.getInetAddress().getHostName()+"-->"+str);
}
// }
bos = new OutputStreamWriter(s.getOutputStream());
ops= new BufferedWriter(bos);
ops.write("我是服务器,成功接收到你的信息!");
ops.flush();
s.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(ops!=null){
try {
ops.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
------多线程的方式------
package dom4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
//Socket通信客户端
class Client implements Runnable{
public void run() {
Socket socket = null;
BufferedWriter bw = null;
BufferedReader br = null;
BufferedReader bd = null;
try {
socket = new Socket(InetAddress.getByName("192.168.10.15"), 9013);
bw = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
while ((str = br.readLine()) != null) {
if (str.equalsIgnoreCase("exit") || str.equalsIgnoreCase("e")) {
System.out.print("发送完毕");
bw.write(str);
bw.flush();
socket.shutdownOutput();
break;
}
bw.write(str);
bw.newLine();
bw.flush();
}
bd=new BufferedReader(new InputStreamReader(socket.getInputStream(),"GBK"));
String str1=null;
while((str1=bd.readLine())!=null){
System.out.println("收到来自于:"+socket.getInetAddress().getHostName()+"-->"+"的反馈信息!"+str1);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bd != null) {
try {
bd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package dom4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
//服务器接收信息,打印到控制台,并返回信息
class Server implements Runnable{
public void run() {
ServerSocket ss=null;
BufferedReader br=null;
BufferedWriter bw=null;
try {
ss = new ServerSocket(9012);
br = new BufferedReader(new InputStreamReader(ss.accept().getInputStream(),"GBK"));
String str=null;
while((str=br.readLine())!=null){
if(str.equalsIgnoreCase("exit") || str.equalsIgnoreCase("e")){
System.out.println("收到来自"+ss.accept().getInetAddress().getHostName()+"发送的消息"+str);
break;
}
System.out.println("收到来自"+ss.accept().getInetAddress().getHostName()+"发送的消息"+str);
}
ss.accept().shutdownInput();
bw = new BufferedWriter(new OutputStreamWriter(ss.accept().getOutputStream(),"GBK"));
bw.write("我是服务器,已接收到消息!");
bw.flush();
ss.accept().shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package dom4;
public class TestTCP {
public static void main(String[] args) {
Client c=new Client();
Server s=new Server();
Thread t1=new Thread(s);
t1.setName("服务器");
t1.start();
// try {
// Thread t=new Thread(c);
// t.setName("客户端");
// Thread.sleep(1000);
// t.start();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }*/
}
}
</span>
(尚硅谷)java网络 TCP 2
最新推荐文章于 2021-12-26 16:32:45 发布