首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

大家来找碴-java 流解决方法

2012-02-10 
大家来找碴-java 流服务器packagemainimportjava.io.*importjava.net.*importjava.util.*importenroll

大家来找碴-java 流
服务器
package   main;
import   java.io.*;
import   java.net.*;
import   java.util.*;
import   enroll.EnrollInfo;
public   class   Main  
{
public   static   void   main(String[]   args)  
{
try  
{
//创建连接
ServerSocket   socket   =   new   ServerSocket(7002);
while(true)
{
int   counter=1;
//连接
Socket   incoming=socket.accept();
System.out.println(counter);
Runnable   r=new   ThreadeEchoHand(incoming,counter);
Thread   t=new   Thread(r);
t.start();
counter++;
}
}  
catch   (IOException   e)
{
e.printStackTrace();
}
}
}
class   ThreadeEchoHand   implements   Runnable//线程类
{
private   Socket   incoming;
private   int   counter;
public   ThreadeEchoHand(Socket   incoming,   int   counter)  
{
this.incoming=incoming;
this.counter=counter;
}
public   void   run()  
{
try  
{
try
{
//输入流和输出流连接incoming
InputStream   inStream=incoming.getInputStream();
OutputStream   outStream=incoming.getOutputStream();
//用输入流得到数据
ObjectInputStream   inobj=new   ObjectInputStream(inStream);
//用输出流输出数据
ObjectOutputStream   outobj=new   ObjectOutputStream(outStream);
try  
{
//读一段数据
EnrollInfo   n=(EnrollInfo)inobj.readObject();
n.toString();
}  
catch   (ClassNotFoundException   e)  
{
e.printStackTrace();
}
}
finally{incoming.close();}//关闭流
}  
catch   (IOException   e)  
{
e.printStackTrace();
}
}
}
客户端
package   ceshi;
import   java.io.*;
import   java.net.*;
import   java.sql.*;

import   enroll.EnrollInfo;
public   class   Na  
{

public   Na()  
{
ObjectOutputStream   o=null;
Socket   socket1=null;
try   {
socket1   =   new   Socket( "127.0.0.1 ",   7002);
//输入流和输出流连接incoming
OutputStream   outStream=socket1.getOutputStream();
o=new   ObjectOutputStream(outStream);
EnrollInfo   nn=new   EnrollInfo( "name ", "asas ",new   Date(19820208),   "3423 ",   "3123 ",   "324 ",   "3424 ",   "4234 ");
o.writeObject(nn);
o.flush();
}   catch   (UnknownHostException   e)   {
e.printStackTrace();
}   catch   (IOException   e)   {
e.printStackTrace();
}  
//关闭流
finally{try   {
o.close();
socket1.close();
}   catch   (IOException   e)   {
e.printStackTrace();
}}

}
public   static   void   main(String[]   args)  
{
Na   a=new   Na();
}
}
传送类
package   enroll;

import   java.sql.*;

//注册信息
public   class   EnrollInfo  
{
private   String   name;//昵称
private   String   pass;//密码
private   Date   day;//生日


private   String   sex;//性别
private   String   constellation;//星座
private   String   attribute;//生肖
private   String   dwellingPlace;//省份
private   String   consciousness;//个人说明
public   EnrollInfo(String   name,   String   pass,   Date   day,   String   sex,   String   constellation,   String   attribute,   String   dwellingPlace,   String   consciousness)  
{
this.name   =   name;
this.pass   =   pass;
this.day   =   day;
this.sex   =   sex;
this.constellation   =   constellation;
this.attribute   =   attribute;
this.dwellingPlace   =   dwellingPlace;
this.consciousness   =   consciousness;
}
public   String   getAttribute()   {
return   attribute;
}
public   void   setAttribute(String   attribute)   {
this.attribute   =   attribute;
}
public   String   getConsciousness()   {
return   consciousness;
}
public   void   setConsciousness(String   consciousness)   {
this.consciousness   =   consciousness;
}
public   String   getConstellation()   {
return   constellation;
}
public   void   setConstellation(String   constellation)   {
this.constellation   =   constellation;
}
public   Date   getDay()   {
return   day;
}
public   void   setDay(Date   day)   {
this.day   =   day;
}
public   String   getDwellingPlace()   {
return   dwellingPlace;
}
public   void   setDwellingPlace(String   dwellingPlace)   {
this.dwellingPlace   =   dwellingPlace;
}
public   String   getName()   {
return   name;
}
public   void   setName(String   name)   {
this.name   =   name;
}
public   String   getPass()   {
return   pass;
}
public   void   setPass(String   pass)   {
this.pass   =   pass;
}
public   String   getSex()   {
return   sex;
}
public   void   setSex(String   sex)   {
this.sex   =   sex;
}
public   String   toString()
{
return   "姓名 "+this.name+ "生日 "+this.day;
}
}
好像是说传送数据错误~~请大家告诉我那里错了!拜托了!

[解决办法]
EnrollInfo 这个类要实现Serializable接口.
[解决办法]
public class EnrollInfo implements Serializable
===
未实现Serializable接口的类不能序列化

热点排行