1.通信原理
服务器端 1.先建立一个ServerSocket,等待客户端连接
2.当有客户端连接时,建立连接,为连接多个客户端,我们创建一个线程处理客户端发来的请求
3.完成通信后,服务器关闭此连接
//根据端口号实例化ServerSocket类的一个对象 ServerSocket server = new ServerSocket(8888); while(flag){ // 等待客户机连接进入,进入后实例化一个客户机Socket对象 Socket client = server.accept(); System.out.println("客户机连接成功,客户机的地址是:" + client.getRemoteSocketAddress()); //当有客户机接入时创建线程 Clientuser user=new Clientuser(client,jta); user.start();
客户端:1.根据服务器ip地址和端口号创建Socket
2.连接成功后创建线程处理连接
3.通信完成后断开连接
try { client =new Socket("192.168.1.158",9999); os=client.getOutputStream(); is=client.getInputStream(); Clienter clienter=new Clienter(client); clienter.start(); } catch (Exception e) { e.printStackTrace(); }
2.通信协议
通信协议说白了就是你指定的发送数据和接受数据的规则。
例如发消息时,你可以指定先发送int说明消息的总长度,再发送一个字节长度的数据来表示你发送的是消息不是其他的,再发送int说明接受者的id,最后发送消息体;接收的时候你也按照这个规则依次读取信息。
发文件时,你可以先发送一个int表示文件名,然后再把文件循环读取到固定长度的缓冲区发给服务器,直到读取到文件末尾,这样无论多大的文件都可以发送,发送时间长短就要看你的网速了,昨天在蓝杰测试发个100多k的图片发了好久
(1)一个登陆请求代码示例:
public String login(String msg){ String user_name = this.username.getText().toString(); String pass_word = this.password.getText().toString(); try { byte []data1 = user_name.getBytes(); byte []data2 = pass_word.getBytes(); int total_len = 4+1+4+data1.length+data2.length; int name_len = data1.length; dataout.writeInt(total_len); dataout.writeByte(1); dataout.writeInt(name_len); dataout.write(data1); dataout.write(data2); dataout.flush(); msg = bufferin.readLine(); System.out.println(msg+"hhhhhhhh"); } catch (IOException e) { e.printStackTrace(); } return msg; }
(2)登录验证代码示例:
public void process_login(int total_len) throws SQLException { try { int name_len = dis.readInt(); byte[] data1 = new byte[name_len]; byte[] data2 = new byte[total_len - 9 - name_len]; dis.read(data1); dis.readFully(data2); String username = new String(data1); String password = new String(data2); System.out.println(username + " "); System.out.println(password); // 获取验证结果信息 String msg = query(username, password); if (msg.equals("验证已通过")) { sendMsg("验证已通过"); System.out.println("验证已通过"); } if (msg.equals("密码输入错误")) { sendMsg("密码输入错误"); System.out.println("密码输入错误"); } if (msg.equals("用户不存在")) { sendMsg("用户不存在"); System.out.println("用户不存在"); } } catch (IOException e) { e.printStackTrace(); } }
(3)关于文件的传输我写了个测试程序,详情见附件,欢迎下载
由于程序中我指定默认保存在D盘Download下所以你要有这个文件夹
相关推荐
基于Qt实现局域网Socket通信系统源码。分别有服务端(Server)和客户端(Client) 服务端默认监听本机IP的6666端口 本人实测在linux(需防火墙开启端口)和windows下完美运行。基于Qt实现局域网Socket通信系统源码。...
windows下socket通信代码,带注释,适合初学者;包含客户端和服务器两个文件
socket通信库,c++编写,只需调用几个函数即可完成socket通信过程。简单易用,C#等其他语言可调用.问题沟通可发送邮件至gaocongly@126.com
c语言下进行socket通信
基于Qt实现的TCP socket通信,这是我学习qt socket通信自己写的一个小例子,希望对你有所帮助。整个文件包括服务端和客户端工程代码,具有如下功能: 1. 服务端支持多个客户端与之连接; 2. 服务端支持客户端...
socket通信文件传输 socket通信文件传输 socket通信文件传输 socket通信文件传输
linux C语言 socket通信聊天小程序
什么是socket通信? TCP/IP协议是传输层协议,主要解决数据如何在网络中传输。HTTP是应用层协议,主要解决如何包装数据。 Socket是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。它是网络通信过程中端点的...
Android socket通信聊天,客户端+服务端
C#做服务器,Android做客户端,实现Socket通信
基于C#开放式TCP通信建立与西门子PLC的socket通信示例
Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信Socket通信
文件传输 聊天 socket通信 文件传输 聊天 socket通信 文件传输 聊天 socket通信
C#实现的简单Socket通信。以及AES加密。
一款C#开发的稳定完整版的Socket通信应用实例
这是android socket通信的实例程序,具体请参考: http://blog.csdn.net/htttw/article/details/7574372
matlab环境下socket通信 。网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket。
2个窗口分别模拟服务器端和客户端,基于TCP协议的socket通信,实现互相通信
socket通信代码,包括服务端和客户端程序,服务端和客户端能实现通信
C# Socket通信(winform) 异步发送消息,服务端客户端通信,可以发送消息和文件并进行接收,代码注释详细 C# Socket通信(winform) 异步发送消息,服务端客户端通信,可以发送消息和文件并进行接收,代码注释详细 ...