读书人

帮忙看看这个程序一点连接就没响应1

发布时间: 2012-12-21 12:03:50 作者: rapoo

求助:帮忙看看这个程序,一点连接就没响应1
初次作J#的程序,用途是两台电脑通信,本意是点击连接后就打开一个端口侦听,但一会儿就没响应了。源代码如下:这个是服务器端的程序,请帮帮忙(含代码,由于发贴长度有限制,我把源代码分开在几个帖子里)
package Socket_Server;

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
import java.io.*;
import java.net.*;

/**
* Summary description for Form1.
*/
public class ServerSocketForm extends System.Windows.Forms.Form implements Runnable
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button_send;
private System.Windows.Forms.Button button_link;
private System.Windows.Forms.CheckBox checkBox_ME;
private System.Windows.Forms.CheckBox checkBox_DME;
private System.Windows.Forms.Button button_unlink;
private System.Windows.Forms.Button button_close;
private System.Windows.Forms.TextBox textBox_send;
private System.Windows.Forms.TextBox textBox_receive;

public ServerSocket server = null ;//服务器Socket
public Socket clientSocket ;//客户端Socket
public boolean flag = true ;//标记是否结束
public boolean send = false ;//标志是否发送
public boolean unsend = false ;//标志是否断开连接
public Thread S_To_C ;//向客户器端发送信息的线程
public BufferedReader sin ;//数据输入缓冲区
public DataOutputStream sout ;//数据输出缓冲区
public InputStream is ;//数据输入流
public OutputStream os ;//数据输出流
/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;

public void ServerStart( )
{
try
{
//初始化端口号
server = new ServerSocket(5678) ;
MessageBox.Show("您设置的端口号是:"+ server.getLocalPort( )) ;
while( flag )
{
//侦听远程计算机是否已经联机
clientSocket = server.accept( ) ;
MessageBox.Show("对方已连接!") ;
//创建输入流
is = clientSocket.getInputStream( ) ;
sin = new BufferedReader(new InputStreamReader( is )) ;
//创建输出流
os = clientSocket.getOutputStream() ;
sout = new DataOutputStream( os ) ;
//创建线程
S_To_C = new Thread( this ) ;
//启动线程,向客户器端发送信息
S_To_C.start() ;
String aline ;
while(( aline = sin.readLine( )) !=null )
{
//接收客户端的数据
textBox_receive.set_Text( aline ) ;
if( unsend )
{
//传输结束,线程中断
flag = false ;
S_To_C.interrupt( ) ;
break ;
}
}
//关闭流
sout.close( ) ;
os.close( ) ;
sin.close( ) ;
is.close( ) ;
//关闭Socket连接
clientSocket.close( ) ;
System.exit( 0 ) ;
}
}
catch( Exception e )
{
MessageBox.Show( e.ToString( )) ;
}
}

[解决办法]
不懂J#,顶起接分

读书人网 >.NET

热点推荐