Socket实现服务器与客户端的交互

.png)
import java.io.*;
import java.net.Socket;
import java.util.Scanner; /**
* Created by Administrator on 2017/6/27.
* 客户端
*/
public class Client {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
//PrintWriter pw = null;
Scanner scanner = null;
InputStream is = null;
BufferedReader br = null;
try {
//创建客户端Socket,指定服务器地址和端口
socket = new Socket("localhost", 8888);
//获取输出流,向服务器发送信息
os = socket.getOutputStream();//字节输出流
// pw = new PrintWriter(os);//将输出流包装为打印流
// pw.write("用户名:cw,密码:0813");
// pw.flush();
System.out.println("客户端,请输入多个字符:");
scanner = new Scanner(System.in);
String str = scanner.next();
os.write(str.getBytes());
os.flush();
//socket.shutdownInput(); //获取输入流,并读取服务器端的响应信息
// is = socket.getInputStream();
// br = new BufferedReader(new InputStreamReader(is));
// String info = null;
// while ((info = br.readLine()) != null) {
// System.out.println("服务器:" + info);
// info=br.readLine();
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (scanner != null) {
scanner.close();
} /* if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/ }
}
}
Server服务器端:
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/** * Created by Administrator on 2017/6/27. * 基于TCP协议的Socket通信,实现用户登陆 * 服务器端 */
public class Server {
public static void main(String[] args) {
try{
//创建一个服务器端Socket,指定并监听端口
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket=null; int count =0;
System.out.println("*****服务器即将启动,等待客户端的连接*****");
//循环监听等待客户端的连接
while(true){
//调用accept()方法开始监听,等待客户端的连接
socket = serverSocket.accept();
//创建一个线程
ServerThread serverThread=new ServerThread(socket);
//启动线程
serverThread.start();
count++;
System.out.println("客户端的数量:"+count);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress()); }
}catch (Exception e){
e.printStackTrace(); }
}
}
连接过程: 根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。
(1)服务器监听:是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。
(2)客户端请求:是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。
(3)连接确认:是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
socket连接过程图解 demo: Client客户端: import java.io.*;
import java.net.Socket;
import java.util.Scanner; /**
* Created by Administrator on 2017/6/27.
* 客户端
*/
public class Client {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
//PrintWriter pw = null;
Scanner scanner = null;
InputStream is = null;
BufferedReader br = null;
try {
//创建客户端Socket,指定服务器地址和端口
socket = new Socket("localhost", 8888);
//获取输出流,向服务器发送信息
os = socket.getOutputStream();//字节输出流
// pw = new PrintWriter(os);//将输出流包装为打印流
// pw.write("用户名:cw,密码:0813");
// pw.flush();
System.out.println("客户端,请输入多个字符:");
scanner = new Scanner(System.in);
String str = scanner.next();
os.write(str.getBytes());
os.flush();
//socket.shutdownInput(); //获取输入流,并读取服务器端的响应信息
// is = socket.getInputStream();
// br = new BufferedReader(new InputStreamReader(is));
// String info = null;
// while ((info = br.readLine()) != null) {
// System.out.println("服务器:" + info);
// info=br.readLine();
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (scanner != null) {
scanner.close();
} /* if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/ }
}
} Server服务器端: import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/** * Created by Administrator on 2017/6/27. * 基于TCP协议的Socket通信,实现用户登陆 * 服务器端 */
public class Server {
public static void main(String[] args) {
try{
//创建一个服务器端Socket,指定并监听端口
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket=null; int count =0;
System.out.println("*****服务器即将启动,等待客户端的连接*****");
//循环监听等待客户端的连接
while(true){
//调用accept()方法开始监听,等待客户端的连接
socket = serverSocket.accept();
//创建一个线程
ServerThread serverThread=new ServerThread(socket);
//启动线程
serverThread.start();
count++;
System.out.println("客户端的数量:"+count);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端的IP:"+address.getHostAddress()); }
}catch (Exception e){
e.printStackTrace(); }
}
} ServerThread线程: import java.io.*;
import java.net.Socket;
import java.util.Scanner; /**
* Created by Administrator on 2017/6/27.
* 服务器线程处理类
*/
public class ServerThread extends Thread {
//和本线程相关的Socket
Socket socket = null; public ServerThread(Socket socket) {
this.socket = socket;
} //线程执行的操作,响应客户端的请求2
@Override
public void run() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = null;
//PrintWriter pw = null;
Scanner scanner = null;
try {
System.out.println("*****线程****");
//获取输入流,并读取客户端信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info = null;
while ((info = br.readLine()) != null) {//循环读取客户端信息
System.out.println("来自客户端:" + info);
br.readLine();
}
// socket.shutdownInput(); //socket.shutdownInput(); //获取输出流,响应服务端的请求
os = socket.getOutputStream();
System.out.println("服务器,请输入多个字符:");
scanner = new Scanner(System.in);
String str = scanner.next();
os.write(str.getBytes()); // os.flush();
// pw = new PrintWriter(os);
//pw.write("elcome!");
// pw.flush(); } catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (scanner != null) {
scanner.close();
} if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Socket实现服务器与客户端的交互的更多相关文章
- 实现服务器和客户端数据交互,Java Socket有妙招
摘要:在Java SDK中,对于Socket原生提供了支持,它分为ServerSocket和Socket. 本文分享自华为云社区<Java Socket 如何实现服务器和客户端数据交互>, ...
- C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法
C#调用接口注意要点 在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存c ...
- socket,模拟服务器、客户端通信
服务器代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;u ...
- 基于asp.net MVC 的服务器和客户端的交互(一)
架构思想 三层架构 提出了一种基于ASP.NET开发方式的三层架构的Web应用系统构造思想.其基本内容是:将面向对象的UML建模与Web应用系统开发 相结合,将整个系统分成适合ASP.NET开发方式的 ...
- Socket通信——服务器和客户端相互通信
所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求. Socket和S ...
- Linux系统编程(34)—— socket编程之TCP服务器与客户端的交互
前面几篇中实现的client每次运行只能从命令行读取一个字符串发给服务器,再从服务器收回来,现在我们把它改成交互式的,不断从终端接受用户输入并和server交互. /* client.c */ #in ...
- java socket 单服务器多客户端实时通信
想用JAVA做一个服务器,请问怎么利用TCP和线程,实现多个客户端同时在线,能与服务器进行交互? 服务器监听端口 做个无限循环 接到一个连接就创建一个通道线程,并将通道线程存储到一个list集合中 1 ...
- 基于asp.net MVC 的服务器和客户端的交互(三)之客户端请求响应
一.分析 WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: GET 用于获取 URI 资源的进行展示,GET ...
- 基于asp.net MVC 的服务器和客户端的交互(二)之获取Oauth 2.0认证权限
基本Web API的ASP.NET的Oauth2认证 增加Token额外字段 增加Scope授权字段 持久化Token 设计Token的时间间隔 刷新Token后失效老的Token 自定义验证[重启I ...
随机推荐
- 达芬奇TI DVSDK之视频数据流过程分析
作者:openwince@gmail.com 博客:http://www.cnblogs.com/tinz 本文的copyright归openwince@gmail.com所有,使用GPL发布, ...
- CHM编写软件
工具选择 1. HTML编辑工具:就是用什么软件写文档的问题.一直都是做.NET开发,所以选择HTML编辑也大都在MS阵营里选.主要有以下几种HTML编辑器. (1) Dr ...
- jsp数据库连接大全和数据库操作封装到Javabean
一.jsp连接Oracle8/8i/9i数据库(用thin模式) testOracle.jsp如下: <%@ page contentType="text/html;charset=g ...
- Spring-3.2.5 + Quartz-2.2.1 集群实例(Tomcat+Memcached+Quartz集群session共享)
本例中我启动了两个Tomcat作效果测试,先看效果图: 现在我们关闭一个Tomcat 注意红线的位置和设置的参数有关 #org.quartz.jobStore.clusterCheckinInterv ...
- eclipse Reference 功能之——项目之间的引用
i'm sorry, i forgot this article where i found. that it is referenced. 以前也研究过Eclipse里Web Project引用Ja ...
- 2008技术内幕:T-SQL语言基础 单表查询摘记
这里的摘抄来自<Microsoft SQL Server 2008技术内幕:T-SQL语言基础>,书中用到的案例数据库是这个 TSQLFundamentals2008 ,官网给出的连接是这 ...
- Ubuntu 16.04 重置密码
忘记了你的Ubuntu用户密码,登录不了系统:不要紧,在Ubuntu上重置密码是很简单的,即使你忘记了用户名. #1 进入Recovery Mode Recovery Mode即恢复模式:在Grub启 ...
- 【BZOJ】【1006】【HNOI2008】神奇的国度
弦图最小染色/MCS算法 Orz PoPoQQQ (UPD:ydc的写法好像更熟悉一些……(类似堆优化的Dij啊~ 先留个坑……明天再看一看……感觉好神奇>_<(完美消除序列之于弦图 就 ...
- 我所遭遇过的游戏中间件--Apex
我所遭遇过的游戏中间件--Apex Apex是PhysX的扩展中间件,它是在PhysX的基础上封装了一层.用于实现布料,粒子,破碎这三种物理效果.我只研究其布料处理.使用Apex做物理最大的好处是:它 ...
- Trapping Rain Water leetcode java
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...