Java TCP communication: Java ServerSocket class and socket class
•
Java
ServerSocket class
Construction method of ServerSocket
try
{
ServerSocket serverSocket=new ServerSocket(8111);
}
catch(IOException e)
{
e.printStackTrace();
}
Common methods of ServerSocket
Example 1
public static void main(String[] args)
{
try
{
//在8888端口创建一个服务器端套接字
ServerSocket serverSocket=new ServerSocket(8888);
System.out.println("服务器端Socket创建成功");
while(true)
{
System.out.println("等待客户端的连接请求");
//等待客户端的连接请求
Socket socket=serverSocket.accept();
System.out.println("成功建立与客户端的连接");
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
服务器端S.ocket创違成功 等待客户端的连接请求
Socket class
Construction method of socket
Common methods of socket
Example 2
public static void main(String[] args)
{
Socket socket=null;
OutputStream out=null;
InputStream in=null;
String serverIP="127.0.0.1"; //服务器端 IP 地址
int port=5000; //服务器端端口号
}
socket=new Socket(serverIP,port); //建立连接
out=socket.getOutputStream(); //发送数据
out.write("我是客户端数据 ".getBytes());
byte[] b=new byte[1024];
in=socket.getInputStream();
int len=in.read(b);
System.out.println(" 服务器端的反馈为:"+new String(b,len));
in.close(); out.close(); socket.close();
ServerSocket ServerSocket=null; Socket socket=null; InputStream in=null; OutputStream out=null; int port=5000;
ServerSocket=new ServerSocket(port); //创建服务器套接字
System.out.println("服务器开启,等待连接。。。");
socket=ServerSocket.accept(); //获得连接
//接收客户端发送的内容
in=socket.getInputStream();
byte[] b=new byte[1024];
int len=in.read(b);
System.out.println("客户端发送的内容为:"+new String(b,len));
out=socket.getOutputStream();
out.write("我是服务器端".getBytes());
in.close(); out.close(); ServerSocket.close(); socket.close();
服务器开启,等待连接。。。
服务器开启,等待连接。。。 客户端发送的内容为:我是客户端数据
客户端的反馈为:我是服务器端
Simple communication between client and server
public class SocketDemo
{
public static void main(String[] args)
{
Socket socket=null;
PrintWriter out=null;
BufferedReader in=null;
String serverIP="127.0.0.1"; //服务器端ip地址
int port=5000; //服务器端端口号
try
{
socket=new Socket(serverIP,port);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream(),true);
while(true)
{
int number=(int)(Math.random()*10)+1;
System.out.println("客户端正在发送的内容为:"+number);
out.println(number);
Thread.sleep(2000);
}
}
catch(IOException | InterruptedException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package ch16;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.socket;
public class SocketDemoServer1
{
public static void main(String[] args)
{
ServerSocket serverSocket=null;
Socket clientSocket=null;
BufferedReader in=null;
int port=5000;
String str=null;
try
{
serverSocket=new ServerSocket(port); //创建服务器套接字
System.out.println("服务器开启,等待连接。。。");
clientSocket=serverSocket.accept();// 获得链接
//接收客户端发送的内容
in=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while(true)
{
str=in.readLine();
System.out.println("客户端发送的内容为:"+str);
Thread.sleep(2000);
}
}
catch(IOException | InterruptedException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
客户端正在发送的内容为:10 客户端正在发送的内容为:5 客户端正在发送的内容为:10 客户端正在发送的内容为:4 客户端正在发送的内容为:3
服务器幵启,等待连接。。。 客户端发送的内容为:7 客户端发送的内容为:2 客户端发送的内容为:10 客户端发送的内容为:5 客户端发送的内容为:10
Transfer object data
Example 3
package ch16;
public class User implements java.io.Serializable
{
private String name;
private String password;
public User(String name,String password)
{
this.name=name;
this.password=password;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getpassword()
{
return password;
}
public void setPassword(String password)
{
this.password=password;
}
}
package ch16;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.socket;
public class MyServer
{
public static void main(String[] args) throws IOException
{
// 监听10000端口
ServerSocket server=new ServerSocket(10000);
while(true)
{
//接收客户端的连接
Socket socket=server.accept();
//调用客户端的数据处理方法
invoke(socket);
}
}
private static void invoke(final Socket socket) throws IOException
{
//开启一个新线程
new Thread(new Runnable()
{
public void run()
{
//创建输入流对象
ObjectInputStream is=null;
//创建输出流对象
ObjectOutputStream os=null;
try
{
is=new ObjectInputStream(socket.getInputStream());
os=new ObjectOutputStream(socket.getOutputStream());
//读取一个对象
Object obj = is.readObject();
//将对象转换为User类型
User user=(User) obj;
//在服务器端输出name成员和password成员信息
System.out.println("user: "+user.getName()+"/"+user.getpassword());
//修改当前对象的name成员数据
user.setName(user.getName()+"_new");
//修改当前对象的password对象数据
user.setPassword(user.getpassword()+"_new");
//将修改后的对象输出给客户端
os.writeObject(user);
os.flush();
}
catch(IOException|ClassNotFoundException ex)
{
ex.printStackTrace();
}
finally
{
try
{
//关闭输入流
is.close();
//关闭输出流
os.close();
//关闭客户端
socket.close();
}
catch(Exception ex){}
}
}
}).start();
}
}
package ch16;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.socket;
public class MyClient
{
public static void main(String[] args) throws Exception
{
//循环100次
for(int i=0;i<100;i++)
{
//创建客户端Socket
Socket socket=null;
//创建输入流
ObjectOutputStream os=null;
//创建输出流
ObjectInputStream is=null
try
{
//连接服务器
socket=new Socket("localhost",10000);
//接收输出流中的数据
os=new ObjectOutputStream(socket.getOutputStream());
//创建一个User对象
User user=new User("user_"+i,"password_"+i);
//将User对象写入输出流
os.writeObject(user);
os.flush();
//接收输入流中的数据
is=new ObjectInputStream(socket.getInputStream());
//读取输入流中的数据
Object obj=is.readObject();
//如果数据不空则转换成User对象,然后输出成员信息
if(obj!=null)
{
user=(User) obj;
System.out.println("user: "+user.getName()+"/"+user.getpassword());
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
//关闭输入流
is.close();
//关闭输出流
os.close();
//关闭客户端
socket.close();
}
catch(Exception ex) {}
}
}
}
}
user:user_86_nevj/password_86_new user:user_87_new/password_87_new user:user_88_new/password_88_new user:user_89_new/password_89_new user:user_90_new/password_90_new user:user_91_new/password_91_new user:user_92_new/password_92_new user:user_93_new/password_93_new user:user_94_new/password_94_new user:user_95_new/password_95_new user:user_96_new/password_96_new user:user_97_new/password_97_new user:user_98_new/password_98_new user:user_99_new/password_99_new
user:user_86/password_86 user:user_87/password_87 user:user_88/password_88 user:user_89/password_89 user:user_90/password_90 user:user_91/password_91 user:user_92/password_92 user:user_93/password_93 user:user_94/password_94 user:user_95/password_95 user:user_96/password_96 user:user_97/password_97 user:user_98/password_98 user:user_99/password_99
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
