参考:https://blog.csdn.net/lucas_up/article/details/79751443
https://www.cnblogs.com/wangcq/p/3520400.html
先从服务器端说起。服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接。在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了。客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束。
获取主机信息
package SYA;
import java.net.*;
public class Host
{
public InetAddress ipa;
public Host()
{
try
{
ipa=InetAddress.getByName(null);//获取本机的地址
//System.out.println("Host Name:"+ipa.getHostName());
//System.out.println("Host IP:"+ipa.toString());
//System.out.println("Local Host Name:"+ipa.getLocalHost());
}
catch(UnknownHostException e)
{
System.out.println(e.toString());
}
}
}
客户端程序
package SYA;
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args)
{
System.out.println("我是客户端");
String str;
try
{
Host h=new Host();
Socket socket=new Socket(h.ipa,8000);
System.out.println("Socket:"+socket);
InputStream fin=socket.getInputStream();
OutputStream fout=socket.getOutputStream();
//建立数据流
InputStreamReader in=new InputStreamReader(fin);
BufferedReader jin=new BufferedReader(in);
PrintStream out=new PrintStream(fout);
InputStreamReader in_out=new InputStreamReader(System.in);
BufferedReader jout=new BufferedReader(in_out);
while(true)
{
System.out.println("发送字符串");
str=jout.readLine();
out.println(str);
if(str.equals("end"))break;
System.out.println("等待服务器消息");
str=jin.readLine();
System.out.println("服务器消息:"+str);
if(str.equals("end"))break;
}
}
catch(Exception e)
{
}
}
public void fun ()
{
try {
Socket s= new Socket("localhost",7522);
OutputStream os= (OutputStream) s.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter pw= new PrintWriter(osw,true);
pw.println("你好");
InputStream is = s.getInputStream();
InputStreamReader isw = new InputStreamReader (is);
BufferedReader br= new BufferedReader(isw);
String mess= br.readLine();
System.out.println(mess);
}catch( Exception e ){
}
}
}
服务器端程序
package SYA;
import java.net.*;
import java.io.*;
public class Server
{
public static final int port=8000;
public static void main(String[] args)
{
String str;
System.out.println("我是服务端");
try
{
ServerSocket server=new ServerSocket(port);//创建线程监听对象
System.out.println("started:"+server);
Socket socket=server.accept();//负责C/S通信的socket对象
System.out.println("Socket:"+socket);
//socket的输入输出流
InputStream fin=socket.getInputStream();
OutputStream fout=socket.getOutputStream();
//C/S数据流
InputStreamReader in=new InputStreamReader(fin);
BufferedReader jin=new BufferedReader(in);
//S/C输出
PrintStream out= new PrintStream(fout);
InputStreamReader in_out=new InputStreamReader(System.in);
BufferedReader jout=new BufferedReader(in_out);
while(true)
{
System.out.println("等待客户端消息");
str=jin.readLine();
System.out.println("客户端输入:"+str);
if(str.equals("end")){break;}
System.out.print("服务端发送:");
str=jout.readLine();
out.println(str);//向客户端发送
if(str.equals("end")){break;}
}
socket.close();
}
catch(Exception e)
{
System.out.println("异常");
}
}
public void fun()
{
try{
ServerSocket ss=new ServerSocket (7522);//7522是端口号,我们设计端口号要避开8080,1433,1521,3306这几个端口
System.out.println("监听前");
Socket s=ss.accept();//服务器端运行到这会停下,等待客户端
/*System.out.println(s.getInputStream().read());*/
InputStream is= s.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader (isr);//字节流转换成字符流
String name=br.readLine();
System.out.println(name);
OutputStream os= s.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter pw=new PrintWriter(osw,true);
pw.println("welcom"+name);
}catch (Exception e){
}
}
}
运行结果: