服务器
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;//生日