在线等,socket通信!
客户端和服务器相互通信,并且不至一次,服务器可以收到客户端发来的消息;
在客户端的写了个receiveCallbackMessage();用于接受服务器返回的数据,并解析做不同的处理(还没有做),这个函数加在客户端的构造函数里,有这句话就报错:java.net.SocketException: Connection reset 去掉之后服务器就可以收到客户端的数据。请高手指点,解决了马上结贴
服务器端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import java.lang.String;
public class ServerSocketOp
{
/**
* @param args
*/
private ServerSocket serversocket;
private Socket socket = null;
private int port ;
protected static Connection dbConn;
protected static Statement stm;
public enum types{application,check,made,cut};
public ServerSocketOp(String port)
{
//监听端口,
this.port = Integer.parseInt(port);
System.out.println("接收到的端口号port:" + this.port);
try
{
serversocket = new ServerSocket(8000); //第101个用户请求会被拒绝
System.out.println("端口已打开,准备接受数据!");
receiveMessgae();
} catch (IOException e)
{
e.printStackTrace();
}catch (Exception e)
{
e.printStackTrace();
}
}
public void receiveMessgae()throws Exception
{
while(true)
{
try
{
socket = serversocket.accept();
InputStream in = socket.getInputStream();
System.out.println("新的连接建立:" + socket.getInetAddress() + "," +socket.getPort());
System.out.println("服务器接收到的数据如下:");
byte [] bt= new byte[150];
in.read(bt); //接到的字节读入字节数组bb
//in.close();
String str = new String(bt); //字节数组转换整字符串
//byte[] bt2 = new sun.misc.BASE64Decoder().decodeBuffer(str); //Base64解码
//String str2 = new String(bt2); // 还原成字符串
System.out.println(str);
String [] info = str.split(";");
System.out.println(info[0]);
System.out.println(info[1]);
//由info[0]的值判断操作类型
if(info[0].equals(types.application.toString()))
{
System.out.println("进入申请过程!!");
appAc(info[1]);
}else if(info[0].equals("check"))
{
System.out.println("进入审核过程!!");
checkAc(info[1]);
}else if(info[0].equals("made"))
{
}else if(info[0].equals("cut"))
{
}else if(info[0].equals("search"))
{
}else
{
}
//Thread.sleep(5000); //休眠5秒开始接受数据
socket.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
private void appAc(String str)
{
int state = 0;
Date date = new Date(System.currentTimeMillis());
String infoBase64 = new sun.misc.BASE64Encoder().encode(str.getBytes());
try
{
//stm = dbConn.createStatement();
//String sql = "INSERT INTO T_certinfo (USERINFO,CERTSTATE,INPUTTIME) values ('"+ infoBase64 +"','"+ state +"','"+ date +"' )" ;
//stm.executeUpdate(sql);
System.out.println("录入数据库成功!");
String stt = "申请成功,等待审核!";
OutputStream out = socket.getOutputStream();
out.write(stt.getBytes());
out.flush();
System.out.println("服务器发回数据给客户端!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void checkAc(String str)
{
String[] date = str.split("@");
System.out.println("审核时间段是:" + date[0] + "---" + date[1]);
}
public static void main(String[] args)
{
String str = "8000";
ServerSocketOp soc = new ServerSocketOp( str);
//soc.connet(port);
}
}
客户端:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Hashtable;
import com.design.manager.managerclient.types;
public class SocketOperation
{
/**
* @param args
*/
private String ip = "";
private int port ;
public Socket socket;
public enum types{application,check,made,cut};
public SocketOperation(String ip ,String port)
{
this.ip = ip;
this.port = Integer.parseInt(port);
try
{
socket = new Socket(this.ip,this.port);
System.out.println(socket);
receiveCallbackMessage();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void sendAppMessage(String str)throws Exception
{
System.out.println("客户端发送申请数据");
System.out.println(str);
OutputStream out = socket.getOutputStream();
out.write(str.getBytes());
out.flush();
}
public void sendcheckMessage(String str)
{
try
{
OutputStream out2 = socket.getOutputStream();
out2.write(str.getBytes());
out2.flush();
//out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void receiveCallbackMessage() throws IOException
{
System.out.println("客户端开始接收服务器的返回数据:");
InputStream is = socket.getInputStream();
byte [] bt= new byte[150];
is.read(bt); //接到的字节读入字节数组bb
String str = new String(bt); //字节数组转换整字符串
System.out.println(str);
}
public static void main(String[] args) throws Exception
{
String ip = "127.0.0.1";
String port = "8000";
String h = types.application + ";" +"中国"+ "@" + "guizhou" + "@" + "guiyang " + "@" + "ajsdah" + "@" + "kashjh"+ ";" ;
// h = new sun.misc.BASE64Encoder().encode(h.getBytes());
SocketOperation so = new SocketOperation( ip,port);
so.sendAppMessage(h);
}
}