最近做了个小项目,用到了wifi连接,通过wifi去控制一个wifi设备。通常是使用ad-hoc,但是目前android还不支持,只能通过补丁软件支持。所有采用了局域网控制的方式。
其中还涉及到socket编程。短信接口调用。
写这个日子的原因仅限于收藏。由于保密协议代码无法共享,请谅解。
附带:
两个服务端测试程序,c#和java的
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread
{
private Server() throws IOException {
CreateSocket();
//创建Socket服务器
}
public void run() {
Socket client;
String txt;
try {
while (true)
//线程无限循环,实时监听socket端口
{
client=ResponseSocket();
//响应客户端链接请求。。
while(true)
{
txt=ReceiveMsg(client);
System.out.println(txt);
//链接获得客户端发来消息,并将其显示在Server端的屏幕上
SendMsg(client,":FRVC06");
//向客户端返回消息
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
private ServerSocket server = null;
private static final int PORT = 8888;
private BufferedWriter writer;
private BufferedReader reader;
private void CreateSocket() throws IOException
{
server = new ServerSocket(PORT, 100);
System.out.println("Server starting..");
}
private Socket ResponseSocket() throws IOException
{
Socket client = server.accept();
System.out.println("client connected..");
return client;
}
private void CloseSocket(Socket socket) throws IOException
{
reader.close();
writer.close();
socket.close();
System.out.println("client closed..");
}
private void SendMsg(Socket socket,String Msg) throws IOException
{
writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
writer.write(Msg+"\n");
writer.flush();
}
private String ReceiveMsg(Socket socket) throws IOException
{
reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println("server get input from client socket..");
String txt="Sever send:"+reader.readLine();
return txt;
}
public static void main(final String args[]) throws IOException {
Server yaochatserver = new Server();
if (yaochatserver != null) {
yaochatserver.start();
}
}
}
vs2008版本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public System.Net.Sockets.TcpListener listener;
static void Main(string[] args)
{
Program p = new Program();
p.StartServer();
}
public void StartServer()
{
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string host = System.Net.Dns.GetHostName();
int port = 2000;
IPAddress ip = new IPAddress(new byte[] { 172, 22, 13, 205 });
listener = new TcpListener(ip, 8888);
try
{
listener.Start();
}
catch
{
return;
}
System.Net.Sockets.TcpClient tcp = listener.AcceptTcpClient();
System.Net.Sockets.NetworkStream ns = tcp.GetStream();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
while (true)
{
Console.WriteLine("starting......");
byte[] resBytes = new byte[1024];
ns.Read(resBytes, 0, resBytes.Length);
string receiveStr = System.Text.Encoding.ASCII.GetString(resBytes).TrimEnd('\0');
int receiveLength = receiveStr.Length;//接受数据长度
Console.WriteLine("######" + receiveStr + "%%%%" + receiveLength);
}
}
public void StopServer()
{
listener.Stop();
}
}
}
其中还涉及到socket编程。短信接口调用。
写这个日子的原因仅限于收藏。由于保密协议代码无法共享,请谅解。
附带:
两个服务端测试程序,c#和java的
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread
{
private Server() throws IOException {
CreateSocket();
//创建Socket服务器
}
public void run() {
Socket client;
String txt;
try {
while (true)
//线程无限循环,实时监听socket端口
{
client=ResponseSocket();
//响应客户端链接请求。。
while(true)
{
txt=ReceiveMsg(client);
System.out.println(txt);
//链接获得客户端发来消息,并将其显示在Server端的屏幕上
SendMsg(client,":FRVC06");
//向客户端返回消息
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
private ServerSocket server = null;
private static final int PORT = 8888;
private BufferedWriter writer;
private BufferedReader reader;
private void CreateSocket() throws IOException
{
server = new ServerSocket(PORT, 100);
System.out.println("Server starting..");
}
private Socket ResponseSocket() throws IOException
{
Socket client = server.accept();
System.out.println("client connected..");
return client;
}
private void CloseSocket(Socket socket) throws IOException
{
reader.close();
writer.close();
socket.close();
System.out.println("client closed..");
}
private void SendMsg(Socket socket,String Msg) throws IOException
{
writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
writer.write(Msg+"\n");
writer.flush();
}
private String ReceiveMsg(Socket socket) throws IOException
{
reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println("server get input from client socket..");
String txt="Sever send:"+reader.readLine();
return txt;
}
public static void main(final String args[]) throws IOException {
Server yaochatserver = new Server();
if (yaochatserver != null) {
yaochatserver.start();
}
}
}
vs2008版本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public System.Net.Sockets.TcpListener listener;
static void Main(string[] args)
{
Program p = new Program();
p.StartServer();
}
public void StartServer()
{
System.Text.Encoding enc = System.Text.Encoding.ASCII;
string host = System.Net.Dns.GetHostName();
int port = 2000;
IPAddress ip = new IPAddress(new byte[] { 172, 22, 13, 205 });
listener = new TcpListener(ip, 8888);
try
{
listener.Start();
}
catch
{
return;
}
System.Net.Sockets.TcpClient tcp = listener.AcceptTcpClient();
System.Net.Sockets.NetworkStream ns = tcp.GetStream();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
while (true)
{
Console.WriteLine("starting......");
byte[] resBytes = new byte[1024];
ns.Read(resBytes, 0, resBytes.Length);
string receiveStr = System.Text.Encoding.ASCII.GetString(resBytes).TrimEnd('\0');
int receiveLength = receiveStr.Length;//接受数据长度
Console.WriteLine("######" + receiveStr + "%%%%" + receiveLength);
}
}
public void StopServer()
{
listener.Stop();
}
}
}