package project.robot.network;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import project.robot.BluetoothConnection;
import android.os.AsyncTask;
public class TCPServer extends AsyncTask<Void, Integer, Void> {
int port; //Port on which server is running
String clientIP; //IP address of remote client
ServerSocket serverSocket; //Server Socket
Socket clientSocket; //Socket connected to client
DataOutputStream out; //Output stream object to send data
DataInputStream in; //Input Stream object to receive data
boolean flag;
boolean videoFlag; //Used to toggle video
private VideoThread vthread; //Video Thread Object
public static BluetoothConnection conn;
/*
* Starts a TCP Server which listens to incoming connections
*/
public TCPServer(int port) {
this.port = port;
videoFlag = false;
vthread = null;
}
@Override
protected Void doInBackground(Void... arg0) {
int msg;
//Initiating server socket
try {
serverSocket = new ServerSocket(port);
flag = true;
} catch (IOException e1) {
// TODO Auto-generated catch block
System.out.println("Tcpserver: unable to bind socket");
e1.printStackTrace();
flag = false;
}
System.out.println("TCPServer: Server started");
while(flag) {
try {
//Accepting incoming connection
clientSocket = serverSocket.accept();
clientIP = clientSocket.getInetAddress().getHostAddress();
System.out.println("TCPServer: Connected to client at " + clientIP);
//Getting input and output streams
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
msg = 1;
//Start VideoThread
try {
vthread = new VideoThread(clientIP);
vthread.startVideo();
System.out.println("TCPServer: Video started");
videoFlag = true;
} catch (Exception e) {
e.printStackTrace();
vthread = null;
System.out.println("TCPServer: Error while starting video");
}
//Reading input commands and signaling processing function
while(flag && msg != 0) {
msg = in.readInt();
publishProgress(msg);
}
//Stopping video streaming if running
if(vthread != null) {
vthread.stopVideo();
vthread = null;
}
System.out.println("TCPServer: Video stopped");
//Closing Connection to client
clientSocket.close();
clientSocket = null;
System.out.println("TCPServer: Closed connection to host at " + clientIP);
clientIP = "null";
} catch (IOException e) {
if(clientSocket != null) {
clientSocket = null;
}
flag = false;
System.out.println("TCPServer: Error while accepting or closing client connection");
}
}
//Closing serverSocket
if(serverSocket != null) {
try {
serverSocket.close();
System.out.println("TCPServer: Server Socket Closed");
} catch (IOException e) {
e.printStackTrace();
System.out.println("TCPServer: Error while closing socket");
}
serverSocket = null;
}
System.out.println("TCPServer: Server stopped");
return null;
}
private void sendSignal(int signal) {
if(conn != null)
conn.send(signal);
else
System.out.println("TCPServer: null conn, can't send value");
}
@Override
protected void onProgressUpdate(Integer... integers) {
//Method is called every time a publishProgress is called from doInBackground
for(Integer integer : integers) {
System.out.println("TCPServer: Message received - " + integer);
switch(integer) {
case 1:
//Enable SMS service
if(vthread != null)
vthread.msgFlag = true;
break;
case 2:
//Move backward
sendSignal(2);
break;
case 4:
//Move left
sendSignal(4);
break;
case 5:
//Stop
sendSignal(5);
break;
case 6:
//Move right
sendSignal(6);
break;
case 7:
//Buzzer toggle
sendSignal(7);
case 8:
//Move forward
sendSignal(8);
break;
case 9:
//Toggle Video
toggleVideo();
break;
default:
System.out.println("TCPServer: Unrecognized instruction : " + integers[0]);
break;
}
}
}
/*
* Toggles video streaming state
* if it was on then it will stop it
* else it will start video streaming
*/
private void toggleVideo() {
//If videoFlag is true then stop Video else start video
if(vthread==null)
return;
if(vthread.videoStream) {
vthread.videoStream = false;
System.out.println("TCPServer: Video streamming stopped");
} else {
System.out.println("TCPServer: Video streamming started");
vthread.videoStream = true;
}
}
/*
* `s the server process
*/
public void stop() {
System.out.println("TCPServer: Stopping server");
flag = false;
//Stopping video if it is running
if(vthread != null) {
vthread.stopVideo();
vthread = null;
}
System.out.println("TCPServer: Video stopped");
//Closing server socket
if(serverSocket != null) {
try {
serverSocket.close();
System.out.println("TCPServer: Server Socket Closed");
} catch (IOException e) {
e.printStackTrace();
System.out.println("TCPServer: Error while closing socket");
}
serverSocket = null;
}
}
}
请有人告诉我telnet是如何工作的(或者告诉我在哪里可以找到关于它的教程)。我有上面的代码。我必须将上面的代码(适用于blutooth)更改为telnet代码(适用于wifi)。
答案 0 :(得分:3)
你的问题有点奇怪,就像问“浏览器教程”一样,显示下载网页的代码片段,并询问浏览器的工作原理。
Telnet只是一个可以连接到服务器并发送接收文本的应用程序(如浏览器)。这段代码也是一个连接到服务器并发送接收内容的应用程序(快速浏览后,主要是整数)。
许多代码主要处理连接和传递参数。你感兴趣的东西是数据连接:
//Getting input and output streams
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
稍后
//Reading input commands and signaling processing function
while(flag && msg != 0) {
msg = in.readInt();
publishProgress(msg);
}
以后
private void sendSignal(int signal) {
if(conn != null)
conn.send(signal);
else
System.out.println("TCPServer: null conn, can't send value");
}
您应该首先了解java(tutorial here)中的Socket编程。了解之后,你会明白为什么你的问题有点奇怪。您还可以开始弄清楚如何编写代码以使用其他协议进行连接。是的,在编写代码之前,您可以先使用telnet手动尝试。
如果您确实需要“telnet教程”,可以下载任何使用纯文本连接的RFC,并在telnet上模拟协议。 HTTP,FTP,SMTP和IRC很容易入手。尝试使用telnet发送电子邮件。