我已成功连接两台计算机以在它们之间发送对象。但现在我想在Android手机和电脑之间发送对象。 所以,目前我让我的电脑成为服务器。对于Main_Server类,代码如下所示:
import java.util.Scanner;
public class Main_Server {
private static Server server;
private static Network network;
public static void start(){
network = new Network();
server = new Server();
server.startRunning();
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
network.strang = text;
server.sendMessage(network);
}
public static void showMessage(String text){
System.out.println(text);
}
}
然后我有了Server类:
import java.io.*;
import java.lang.reflect.Array;
import java.net.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.text.StyledEditorKit;
public class Server extends JFrame{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public Server(){
super("Chat");
}
public void startRunning(){
try{
server = new ServerSocket(6789);
Thread thread = new Thread(new Runnable(){
public void run(){
while(true){
try{
waitForConnection();
setupStreams();
whileChatting();
System.out.println("managed");
}catch(IOException eofException){
System.out.println("didnt manage");
}
}
}
});
thread.start();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
System.out.println("waiting for connection");
connection = server.accept();
}
//get steam to send and receive data
private void setupStreams() throws IOException{
output = new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream()));
output.flush();
input = new ObjectInputStream(new BufferedInputStream(connection.getInputStream()));
}
// during the conversation
private void whileChatting()throws IOException{
Main_Server.showMessage("HERE WE GO");
Thread thread;
thread = new Thread(new Runnable(){
@Override
public void run(){
do{
try{
Network netInput=(Network)input.readUnshared();
Main_Server.showMessage(netInput.strang);
}catch(ClassNotFoundException | IOException k){
k.printStackTrace();
}
}while(true);
}
});
thread.start();
}
//close streams and socket after done chatting
//send a message to client
public void sendMessage(Network network){
try{
output.writeUnshared(network);
output.reset();
output.flush();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
}
当我连接计算机时,这段代码可以正常工作,所以我猜我在跟随代码中失败了,这是android设备上的代码。 主类:
package com.example.john.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static Client client;
private static Network network;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
}
public void ButtonOnClick(View v) {
if (v.getId() == R.id.button) {
String address = "192.168.1.208";
System.out.println("WASAP");
client = new Client(address);
network = new Network();
try {
client.startRunning();
} catch (Exception e) {
e.printStackTrace();
}
/*EditText text = (EditText) findViewById(R.id.editText);
network.strang = String.valueOf(text.getText());
Client.sendMessage(network);*/
}
}
}
我知道我已经注释了一些东西。但是,当两台设备连接时,我仍然可以在计算机上看到“HERE WE GO”打印。 这是客户类:
package com.example.john.myapplication;
import java.io.*;
import java.net.*;
public class Client{
private static ObjectOutputStream output;
private ObjectInputStream input;
private String serverIP;
private Socket connection;
//constructor
public Client(String host){
serverIP = host;
}
//connect to server
public void startRunning() throws Exception{
try{
connectToServer();
setupStreams();
whileChatting();
}catch(Exception e){
}
}
//connect to server
private void connectToServer() throws IOException{
connection = new Socket(InetAddress.getByName(serverIP), 6789);
}
private void setupStreams() throws IOException{
output = new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream()));
output.flush();
input = new ObjectInputStream(new BufferedInputStream(connection.getInputStream()));
}
private void whileChatting(){
Thread thread;
thread = new Thread(new Runnable(){
@Override
public void run(){
do{
try{
Network netInput= (Network)input.readUnshared();
//Main_Client.showMessage(netInput.strang);
}catch(ClassNotFoundException | IOException k){
k.printStackTrace();
}
}while(true);
}
});
thread.start();
}
public static void sendMessage(Network network){
try{
output.writeUnshared(network);
output.reset();
output.flush();
}catch(IOException ioException){
}
}
}
我没有向你展示所有的课程,但我认为你能够发现错误。我的猜测是你不能以与计算机完全相同的方式使用套接字。但我希望我只需要替换一些代码就可以完成这项工作。
由于