Java知识整理(十)之SE.TCP通信(Server和Client源代码)
2015-09-19 10:14
363 查看
Socket(套接字):用于描述IP地址和端口,每个服务软件都打开了一个Socket,并绑定到一个端口上。Socket和ServerSocket(用于服务器)类位于java.net包
获取本地地址和端口号:int getLocalPort();String getHostAddress();InetAddress getLocalAddress();
获取网络输入输出流:
Socket相当于一个邮箱,pw.println();把信放到邮箱;br.readLine();从邮箱拿信;
使用Socket完毕后使用void close()方法关闭(会同时关闭输入输出流)
服务器端(Server):创建监听服务-->等待连接--->进行通讯--->关闭连接
public class Server{
private ServerSocket server;
private List<PrintWriter> allOut;
public Server(){
try{
allOut = new ArrayList<PrintWriter>();
server = new ServerSocket(8088);
}catch(Exception e){
e.printStackTrace();
}
}
private synchronized void addOut(PrintWriter pw){
allOut.add(pw);
}
private synchronized void removeOut(PrintWriter pw){
allOut.remove(pw);
}
private synchronized void sendMessageToAllClient(String message){
for(PrintWriter pw:allOut){
pw.println(message);
}
}
public void start(){
try{
while(true){
System.out.println("等待客户端连接....");
Socket socket = server.accept();
ClientHandle clientHandle = new ClientHandle(socket);
Thread t = new Thread(clientHandle);
t.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Server server = new Server();
server.start();
}
private class ClientHandle implements Runnable{
private Socket socket;
private String host;
public ClientHandle(Socket socket){
this.socket=socket;
InetAddress address = socket.getInetAddress();
host = address.getHostAddress();
System.out.println(host+"上线了");
}
public void run(){
PrintWriter pw = null;
try{
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
pw = new PrintWriter(osw,true);
addOut(pw);
InputStream in = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
while((s=br.readLine())!=null){
System.out.println(host+"说:"+s);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
removeOut(pw);
socket.close();
System.out.println(host+"下线了!");
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
客户端(Client):连接服务器-->进行通讯-->关闭连接
public class Client{
private Socket socket = new Socket();
public Client(){
try{
socket = new Socket("127.0.0.1",8088);
}catch(Exception e){
e.printStackTrace();
}
}
public void start(){
try{
ServerMessage message = new ServerMessage();
Thread t = new Thread(message);
t.start();
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(osw,true);
pw.println("服务端你好!");
Scanner sc = new Scanner(System.in);
while(true){
pw.println(sc.nextLine());
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Client client = new Client();
client.start();
}
private class ServerMessage implements Runnable{
public void run(){
try{
InputStream in = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
while((s=br.readLine())!=null){
System.out.println("服务器说"+s);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
获取本地地址和端口号:int getLocalPort();String getHostAddress();InetAddress getLocalAddress();
获取网络输入输出流:
Socket相当于一个邮箱,pw.println();把信放到邮箱;br.readLine();从邮箱拿信;
使用Socket完毕后使用void close()方法关闭(会同时关闭输入输出流)
服务器端(Server):创建监听服务-->等待连接--->进行通讯--->关闭连接
public class Server{
private ServerSocket server;
private List<PrintWriter> allOut;
public Server(){
try{
allOut = new ArrayList<PrintWriter>();
server = new ServerSocket(8088);
}catch(Exception e){
e.printStackTrace();
}
}
private synchronized void addOut(PrintWriter pw){
allOut.add(pw);
}
private synchronized void removeOut(PrintWriter pw){
allOut.remove(pw);
}
private synchronized void sendMessageToAllClient(String message){
for(PrintWriter pw:allOut){
pw.println(message);
}
}
public void start(){
try{
while(true){
System.out.println("等待客户端连接....");
Socket socket = server.accept();
ClientHandle clientHandle = new ClientHandle(socket);
Thread t = new Thread(clientHandle);
t.start();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Server server = new Server();
server.start();
}
private class ClientHandle implements Runnable{
private Socket socket;
private String host;
public ClientHandle(Socket socket){
this.socket=socket;
InetAddress address = socket.getInetAddress();
host = address.getHostAddress();
System.out.println(host+"上线了");
}
public void run(){
PrintWriter pw = null;
try{
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
pw = new PrintWriter(osw,true);
addOut(pw);
InputStream in = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
while((s=br.readLine())!=null){
System.out.println(host+"说:"+s);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
removeOut(pw);
socket.close();
System.out.println(host+"下线了!");
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
客户端(Client):连接服务器-->进行通讯-->关闭连接
public class Client{
private Socket socket = new Socket();
public Client(){
try{
socket = new Socket("127.0.0.1",8088);
}catch(Exception e){
e.printStackTrace();
}
}
public void start(){
try{
ServerMessage message = new ServerMessage();
Thread t = new Thread(message);
t.start();
OutputStream out = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
PrintWriter pw = new PrintWriter(osw,true);
pw.println("服务端你好!");
Scanner sc = new Scanner(System.in);
while(true){
pw.println(sc.nextLine());
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Client client = new Client();
client.start();
}
private class ServerMessage implements Runnable{
public void run(){
try{
InputStream in = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
while((s=br.readLine())!=null){
System.out.println("服务器说"+s);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
相关文章推荐
- java-模拟tomcat服务器
- Linux socket 初步
- Seafile Server本地权限提升漏洞(CVE-2014-5443)
- 从USB安装Ubuntu Server 10.04.3 图文详解
- 使用zabbix监控Nginx活动状态--Part1
- 高效访问Internet-启用ISA Server的缓存
- java socket 注意的地方
- java socket 注意的地方
- windows server域用户提升到本地更高权限组中的方法
- 完美解决SQL Server 安装问题:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- IIS 错误 Server Application Error 详细解决方法
- 在WINXP系统上安装SQL Server企业版的方法
- SQL Server 存储过程的分页
- SQL Server复制需要有实际的服务器名称才能连接到服务器
- SQL Server 2000向SQL Server 2008 R2推送数据图文教程
- SQL server 表操作介绍
- SQL Server 2000 注入防护大全(二)
- SQL Server 2000 注入防护大全(一)
- 怎样把Windows server 2003转换成工作站系统
- Windows Server 2007四月CTP发布 下载