当前位置: 代码迷 >> Java相关 >> 求大神解答 新手遇到的有关socket传输一个类时遇到的有关问题
  详细解决方案

求大神解答 新手遇到的有关socket传输一个类时遇到的有关问题

热度:28   发布时间:2016-04-22 21:02:04.0
求大神解答 新手遇到的有关socket传输一个类时遇到的问题
这是Server端代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;


public class ser_test{
public static void main(String []args) throws Exception
{

ServerSocket socketConnection =new ServerSocket(30000);
System.out.println("start");
Socket socket=socketConnection.accept();
System.out.println("建立连接");

ObjectInputStream in=new ObjectInputStream(socket.getInputStream());


USER user=(USER) in.readObject();

System.out.println("username"+user.getusername());
System.out.println("password"+user.getpassword());


OutputStream os=socket.getOutputStream();
os.write("success".getBytes("utf-8"));

os.close();
socketConnection.close();

}

}



这是client端代码
package com.related.firstactivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Lg_activity2 extends Activity{

private Button login_bt;
private Button register_bt;
private EditText input_username;
private EditText input_password;


public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.login_regest);
        
        login_bt=(Button)findViewById(R.id.loginbt);
        register_bt=(Button)findViewById(R.id.register_bt);
        input_password=(EditText) findViewById(R.id.password);
        input_username=(EditText) findViewById(R.id.username);
        
        login_bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread loginThread = new Thread(new LoginThread());

loginThread.start();
}
});
        
        register_bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent =new Intent();
intent.setClass(Lg_activity2.this, Register.class);
startActivity(intent);
}
});
}


Handler handler= new Handler()
{
 public void handleMessage(Message msg)
 {
 switch(msg.what)
 {
 case 0:
 Toast.makeText(getApplicationContext(), "success", 0).show();
 Intent intent=new Intent(Lg_activity2.this,MainActivity.class);
 startActivity(intent);
 finish();
 break;
 case 1:
 Toast.makeText(getApplicationContext(), "faliure", 0).show();
 break;
 }
 }
};
String loginServer(String username,String password)
{
String reponseMsg="faliuer";
try {
Socket socket=new Socket("192.168.191.2",30000);


ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());

USER user=new USER(username,password);

out.writeObject(user);

out.flush();


BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
reponseMsg=br.readLine();


} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
  相关解决方案