我写的服务端接受时出现异常
java.net.SocketException: Connection reset用java做客户端发送信息,不会出现异常,而接受用c++发送的信息的时候出现以上异常谁能告诉我什么原因啊?
java.net.SocketException: Connection reset用java做客户端发送信息,不会出现异常,而接受用c++发送的信息的时候出现以上异常谁能告诉我什么原因啊?
解决方案 »
- 有关于java socket发送与接收的问题
- 谁能解释一下这是为什么
- 菜鸟..问问题了.. GUI 对一个初学者有没有很大的作用...我的那本书就那个没学得好.有谁能指教一下..谢谢
- 既是Applet,又是Application??
- 请问如何将数据库中以二进制形式存储的图片取出,并在网页上显示出来?
- 静态工厂方法是什么
- 急~~关于JAVA语言反射的问题
- 请刚才回答过“一个关于string[]的问题”的人进来,刚才结贴的时候系统显示参数错误!重新给分!
- 关于执行程序弹出dos窗口的问题!
- Jb7问题集锦之一!如何实现Metal风格窗体
- 求《Thinking in JAVA 第三版》的课后答案,谢谢
- 照抄网上的例子.为什么注册表没有些成功
import java.net.*;
//import java.util.*;
import java.io.*;public class Server
{
public Server()
{
}
public static void main(String[] args) throws IOException
{
System.out.println("------365Maga services --------\n");
System.out.println("Server Starting........");
ServerSocket server = new ServerSocket(10000);
while (true)
{
Socket s = server.accept();
System.out.println("Accepting Connection.......");
System.out.println("A new Thread opened.");
PrintWriter pw = null;
pw = new PrintWriter(s.getOutputStream(),true);
pw.println("sssssssssssssssssfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
new ServerThread(s).start();
}
}
}class ServerThread extends Thread
{
private Socket s;
public ServerThread(Socket server)
{
this.s = server;
}
public void run()
{
BufferedReader br = null;
PrintWriter pw = null;
try
{
InputStreamReader isr;
isr = new InputStreamReader(s.getInputStream());
br = new BufferedReader(isr);
String m = br.readLine();
System.out.println("Receive message: "+m);
//String subM = m.substring(0,5);
//if ("Login".equals(subM))
pw = new PrintWriter(s.getOutputStream(),true);
pw.println("Your message is:"+ m);
//System.out.println(m.split(":"));
System.out.println(m.length());
if(m.startsWith("Login"))
{
System.out.println("one member login.");
}
}
catch(IOException e)
{
System.out.println("Server error:"+ e.toString());
}
finally
{
System.out.println("Connection shuting .......");
try
{
if(br!=null)
{
br.close();
}
if(pw!=null)
{
pw.close();
}
if(s!=null)
{
s.close();
}
}
catch(IOException e)
{
System.out.println("Shuting error:"+e.getMessage());
}
//System.out.println("Services Shuted...\n");
System.out.println("The Thread closed.");
}
}
}
import javax.swing.*;
import java.util.Calendar;/**
* <p>Title: Socketclient</p>
* <p>Description: Socket communication client</p>
* <p>Copyright: JGD 2002.11.08</p>
* <p>Company: JGD</p>
* @author Jiabinbin(JGD's part two team two)
* @version 1.0
*/
public class SocketClient
{
/**
* The programme examines the Socket.ini file Firstly.<br><br>
* If the file existed programme creates and showes main dialog.<br>
* If the file didn't existed the system will exit the programme.
*/
public static void main(String[] args)
{
final String con_startUpFile = "Socket.ini";
final String con_logFile = "socketClient.trc";
final String con_errorDialogTitle = "Error";
final String con_openFileError = "Open Socket.ini error!";
final String con_changeStyleError = "Change the Style error";
File startUpFile = new File(con_startUpFile); if (!startUpFile.exists())
{
JOptionPane.showMessageDialog( null , con_openFileError ,
con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
System.exit(0);
} try
{
javax.swing.UIManager.setLookAndFeel(
javax.swing.UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog( null , con_changeStyleError ,
con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
System.exit(0);
} MainDialog mainDialog = new MainDialog();
mainDialog.show(); try
{
//write log file.
BufferedWriter writeLog =
new BufferedWriter(new FileWriter(con_logFile , true));
writeLog.write("***********************************");
writeLog.newLine();
writeLog.write("[" + Calendar.getInstance().getTime() + "]" +
"Socket.ini is exist,programme run!");
writeLog.newLine();
writeLog.write("-----------------------------------");
writeLog.newLine();
writeLog.close();
}
catch (IOException ex)
{
mainDialog.setLogError();
}
}
}我以前写的和C++通讯的socket,你参考一下!
public void sendMessage()
{
try
{
out = new PrintWriter(socketClient.getOutputStream(), true);
message = "";
message = message + con_sendMessageTitle1 + sendMessageTitle2;
message = message + comboBox.getSelectedItem().toString();
message = message + con_resultTitle1 + textField1.getText() + con_resultTail1;
message = message + con_resultTitle2 + textField2.getText() + con_resultTail2;
message = message + "</" + comboBox.getSelectedItem().toString();
message = message + ">\n" + con_sendMessageTail1 + con_sendMessageTail2;
out.print(message);
out.flush();
log("Sent message is : " + message , 3);
}
catch (IOException e)
{
log(con_sentMessageError , 2);
JOptionPane.showMessageDialog(this , con_sentMessageError ,
con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
socketClose();
}
} /**
* This method's function is that accept the stream from server and
* transfer inFile() to analyse this stream.<br>
* Return null if system could not accept the message at definite time.
*/
public void socketIncept()
{
try
{
String str = new String();
message = "";
in = new BufferedReader(
new InputStreamReader(socketClient.getInputStream()));
while ((str = in.readLine()) != null)
{
message = message + str + "\n";
str = str.trim();
if (str.endsWith(con_sendMessageTail2))
{
break;
}
}
log("Accept message is : " + message , 3);
}
catch (IOException e)
{
log(con_timeOutError , 1);
JOptionPane.showMessageDialog(this , con_timeOutError ,
con_errorDialogTitle , JOptionPane.ERROR_MESSAGE);
socketClose();
return;
}
inFile();
}
http://www.regexlab.com/zh/encoding.htm