客户端接收WIFI发送的数据

1.首先WIFI模块烧入设置,完成模式设置。
2.在myeclipse中写入TCPServer代码
【客户端接收WIFI发送的数据】3.运行TCPServer

#include SoftwareSerial WIFISerial(8,9); // RX, TX//模拟一个软串口 void setup(){ Serial.begin(115200); pinMode(13, OUTPUT); int i=0; WIFISerial.begin(115200); delay(1000); WIFISerial.println("AT"); delay(1000); WIFISerial.println("AT+RST"); //重启 delay(2000); WIFISerial.println("AT+CWMODE=1"); //设置为station模式 delay(1000); WIFISerial.println("AT+RST"); //重启 delay(2000); WIFISerial.println("AT+CWLAP"); //寻找网络 delay(2000); // WIFISerial.println("AT+CWJAP=\"aaa\",\"ghl44826\""); //连接到想要连接的网络 WIFISerial.println("AT+CWJAP=\"aaa\",\"ghl44827\""); delay(2000); Serial.println("connect"); delay(2000); WIFISerial.println("AT+CIFSR"); //返回模块的IP地址 delay(1000); WIFISerial.println("AT+CIPMUX=1"); //打开多连接 delay(1000); //WIFISerial.println("AT+CIPMODE=1"); //透传模式 //delay(1000); WIFISerial.println("AT+CIPSTART=2,\"TCP\",\"10.6.66.199\",20006"); // 接收数据的计算机地址,连接到 TCP 服务器 delay(3000); for(i = 10; i < 100; i++){ WIFISerial.println("AT+CIPSEND=2,4"); delay(1000); //WIFISerial.println("WLQ XK GCP LK"); WIFISerial.println(i); delay(1000); Serial.println("222"); }}void loop(){ int i = 0; //Serial.println("serial output"); if (WIFISerial.available()){ Serial.write(WIFISerial.read()); //Serial.println("ifwifi serial output"); } //Serial.println("only serial output"); if (Serial.available()){ WIFISerial.write(Serial.read()); // Serial.println("if serial output"); } for(i = 10; i < 100; i++){ WIFISerial.println("AT+CIPSEND=2,4"); delay(1000); WIFISerial.println(i); delay(1000); digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } }

package socket; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.Executor; import java.util.concurrent.Executors; public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(20006); while(true) { System.out.println("等待连接"); try { Socket client = server.accept(); System.out.println("连接成功"); try { BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); boolean flag = true; int count = 1; while (flag) { System.out.println("客户端要开始发骚了,这是第 "+ count + "次!"); count++; String line = input.readLine(); System.out.println("客户端说:" + line); FileOutputStream fileOutputStream = null; File file = new File("F://test.txt"); if(!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(file); fileOutputStream.write(line.getBytes("gbk")); fileOutputStream.flush(); fileOutputStream.close(); if (line.equals(0)) { flag = false; //System.out.println("客户端不想玩了!"); } else { //System.out.println("客户端说: "+ line); } } } finally { client.close(); }} finally { server.close(); } }} }

    推荐阅读