下面是套接字的代碼。套接字服務器未與JavaScript套接字客戶端連接
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Random;
public class ChatSocketServer {
private ServerSocket severSocket = null;
private Socket socket = null;
private InputStream inStream = null;
private OutputStream outStream = null;
public ChatSocketServer() {
}
public void createSocket() {
try {
ServerSocket serverSocket = new ServerSocket(3339);
while (true) {
socket = serverSocket.accept();
inStream = socket.getInputStream();
outStream = socket.getOutputStream();
System.out.println("Server Connected");
createReadThread();
createWriteThread();
}
} catch (IOException io) {
io.printStackTrace();
}
}
public void createReadThread() {
Thread readThread = new Thread() {
public void run() {
while (socket.isConnected()) {
try {
byte[] readBuffer = new byte[200];
int num = inStream.read(readBuffer);
if (num > 0) {
byte[] arrayBytes = new byte[num];
System.arraycopy(readBuffer, 0, arrayBytes, 0, num);
String recvedMessage = new String(arrayBytes, "UTF-8");
System.out.println("server received :" + recvedMessage);
} else {
notify();
}
;
//System.arraycopy();
} catch (SocketException se) {
System.exit(0);
} catch (IOException i) {
i.printStackTrace();
}
}
}
};
readThread.setPriority(Thread.MAX_PRIORITY);
readThread.start();
}
public void createWriteThread() {
Thread writeThread = new Thread() {
public void run() {
while (socket.isConnected()) {
try {
while(true){
synchronized (socket) {
Random random = new Random();
int i = random.nextInt(100);
outStream.write(Integer.toString(i).getBytes("UTF-8"));
sleep(100);
}
}/*
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
sleep(100);
String typedMessage = inputReader.readLine();
if (typedMessage != null && typedMessage.length() > 0) {
synchronized (socket) {
outStream.write(typedMessage.getBytes("UTF-8"));
sleep(100);
}
} else {
notify();
}
;*/
//System.arraycopy();
} catch (IOException i) {
i.printStackTrace();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
};
writeThread.setPriority(Thread.MAX_PRIORITY);
writeThread.start();
}
public static void main(String[] args) {
System.out.println("char socket server got started");
ChatSocketServer chatServer = new ChatSocketServer();
chatServer.createSocket();
}
}
上面的代碼工作正常,當我嘗試連接Java套接字客戶端。我不確定爲什麼在連接JavaScript時拋出錯誤。
的JavaScript
try{
var host = "ws://localhost:3339/";
var socket = new WebSocket(host);
console.log(socket.readyState);
socket.onopen = function(){
console.log(socket.readyState);
};
socket.onmessage = function(msg){
console.log(msg.data);
};
socket.onclose = function(){
console.log(socket.readyState);
};
socket.onerror = function(error) {
// connection failed - try polling
console.log(socket.readyState);
};
} catch(exception){
console.log(exception);
}
當我嘗試讀取從JavaScript值時,它引發以下錯誤:
WebSocket connection to 'ws://localhost:3339/' failed: Error during WebSocket handshake: Invalid status line
但我可以能夠通過使用Java代碼的服務器和客戶端之間同步上漲。
一個WebSocket連接需要webSocket服務器,而不僅僅是一個普通的TCP服務器。有一個明確的webSocket協議必須被服務器支持。 – jfriend00
websocket是一個通過http設計的特殊協議,通過增強http頭,你的服務器需要符合web socket協議 – Sanjeev
@Sanjeev http://stackoverflow.com/questions/38752467/error-during-websocket-handshake-unexpected-response -code-404-時,試圖到C –