import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args)
{
try
{
ServerSocket ss =new ServerSocket(8888);//在本机的8888端口监听
System.out.println("服务器端在8888端口监听");
Socket s =ss.accept(); //等待客户端连接到8888端口
System.out.println("已有客户端连接,开始进行通信");
InputStream in =s.getInputStream(); //得到输入流
OutputStream out =s.getOutputStream(); //得到输出流
PrintWriter pw =new PrintWriter(out);
System.out.println("正向客户端发送消息");
pw.write("你好客户端,我是服务器"); //使用printWrite向客户端输出数据
pw.flush(); //强制发送数据
System.out.println("向客户端发送数据完成");
BufferedReader br =new BufferedReader(new InputStreamReader(in));//使用BufferedReader读取客户端打来的数据
String hasRead=null;
hasRead=br.readLine();
System.out.println(hasRead);
br.close();
pw.close();
s.close();
ss.close();
}catch(Exception e)
{
e.printStackTrace();
}}}
------------------------------
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args)
{
byte[] b =new byte[]{127,0,0,1};try
{
InetAddress ip =InetAddress.getByAddress(b);
Socket s =new Socket(ip,8888);
InputStream in =s.getInputStream();
OutputStream out =s.getOutputStream();
BufferedReader br =new BufferedReader(new InputStreamReader(in));
String hasRead=null;
hasRead=br.readLine();
System.out.println("下面是服务器发来的消息");
System.out.println(hasRead);
System.out.println("-----");
System.out.println("向服务器发送消息");
PrintWriter pw=new PrintWriter(out);
pw.write("你好服务器,我是客户端");
pw.flush();
pw.close();
br.close();
s.close();
System.out.println("向客户端发送消息结束");
}catch(Exception e){}
}
}
import java.net.*;
public class Server
{
public static void main(String[] args)
{
try
{
ServerSocket ss =new ServerSocket(8888);//在本机的8888端口监听
System.out.println("服务器端在8888端口监听");
Socket s =ss.accept(); //等待客户端连接到8888端口
System.out.println("已有客户端连接,开始进行通信");
InputStream in =s.getInputStream(); //得到输入流
OutputStream out =s.getOutputStream(); //得到输出流
PrintWriter pw =new PrintWriter(out);
System.out.println("正向客户端发送消息");
pw.write("你好客户端,我是服务器"); //使用printWrite向客户端输出数据
pw.flush(); //强制发送数据
System.out.println("向客户端发送数据完成");
BufferedReader br =new BufferedReader(new InputStreamReader(in));//使用BufferedReader读取客户端打来的数据
String hasRead=null;
hasRead=br.readLine();
System.out.println(hasRead);
br.close();
pw.close();
s.close();
ss.close();
}catch(Exception e)
{
e.printStackTrace();
}}}
------------------------------
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args)
{
byte[] b =new byte[]{127,0,0,1};try
{
InetAddress ip =InetAddress.getByAddress(b);
Socket s =new Socket(ip,8888);
InputStream in =s.getInputStream();
OutputStream out =s.getOutputStream();
BufferedReader br =new BufferedReader(new InputStreamReader(in));
String hasRead=null;
hasRead=br.readLine();
System.out.println("下面是服务器发来的消息");
System.out.println(hasRead);
System.out.println("-----");
System.out.println("向服务器发送消息");
PrintWriter pw=new PrintWriter(out);
pw.write("你好服务器,我是客户端");
pw.flush();
pw.close();
br.close();
s.close();
System.out.println("向客户端发送消息结束");
}catch(Exception e){}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货