简单套接字客户端 - 服务器应用程序: 我把serializeble对象放在ArrayList<>中并将他发送给客户 - >客户端获取此对象并没有。我的数组是空的。
为什么ArrayList<GameObject> arrayList = (ArrayList<GameObject>) in.readObject();
为空?
class GameObject implements Serializable{
public int CX;
public int CY;
public GameObject(int cx, int cy) {
this.CX = cx; this.CY = cy;
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private GameObject[] array;
private ObjectOutputStream out;
Server server;
Client client;
Button btServer;
Button btClient;
Button btSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btServer = (Button) findViewById(R.id.btServer);
btClient = (Button) findViewById(R.id.btClient);
btSend = (Button) findViewById(R.id.btSend);
btServer.setOnClickListener(this);
btClient.setOnClickListener(this);
array = new GameObject[5];
for(int i = 0; i < array.length; ++i)
array[i] = new GameObject(i, 100-i);
btSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
server.send();
}
});
}
@Override
public void onClick(View v) {
btClient.setEnabled(false);
btServer.setEnabled(false);
switch (v.getId())
{
case R.id.btServer:
server = new Server();
server.start();
break;
case R.id.btClient:
client = new Client();
client.start();
break;
}
btSend.setEnabled(true);
}
class Server extends Thread {
public static final int SERVERPORT = 4444;
public void send() {
if(out != null)
{
try
{
ArrayList<GameObject> arrayList = new ArrayList<>();
for(int i = 0; i < arrayList.size(); ++i)
if(array[i] != null) arrayList.add(array[i]);
out.writeObject(arrayList);
out.flush();
}
catch (Exception e)
{
e.printStackTrace();
Log.e("myLogs", e.getMessage());
}
}
}
@Override
public void run() {
super.run();
try {
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
Socket client = serverSocket.accept();
try {
out = new ObjectOutputStream(client.getOutputStream());
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
while (true) {
//ArrayList<GameObject> array = (ArrayList<GameObject>) in.readObject();
//if(array != null) gameMessenger.updateField(array);
}
} catch (Exception e) {
e.printStackTrace();
Log.e("myLogs", e.getMessage());
} finally {
client.close();
}
} catch (Exception e) {
Log.e("myLogs", e.getMessage());
}
}
}
class Client extends Thread {
public static final String SERVERIP = "192.168.1.9";
public static final int SERVERPORT = 4444;
@Override
public void run() {
super.run();
try
{
InetAddress address = InetAddress.getByName(SERVERIP);
Socket socket = new Socket(address, SERVERPORT);
try
{
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
while (true) {
ArrayList<GameObject> arrayList = (ArrayList<GameObject>) in.readObject();
if(array != null) {
for(int i = 0; i < arrayList.size(); ++i)
Log.d("myLogs", String.format("%d. (%d; %d)", i, arrayList.get(i).CX, arrayList.get(i).CY));
Log.d("myLogs", "---");
}
}
}
catch (Exception e)
{
Log.e("myLogs", e.getMessage());
}
finally {
socket.close();
}
}
catch (Exception e)
{
Log.e("myLogs", e.getMessage());
}
}
}
}
activity_main.xml中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.wertyj.deleteittest.MainActivity"
android:gravity="center_vertical|center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SERVER"
android:id="@+id/btServer"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLIENT"
android:id="@+id/btClient"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/btServer"
android:layout_toEndOf="@+id/btServer"
android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/btSend"
android:enabled="false"/>
</LinearLayout>
</LinearLayout>