//Server端:
public class TCPServer()
{
//调用方法createUserInterface()
createUserInterface();
//用try和catch实现异常的捕获和抛出
try
{
//在与客户端的连接没有建立之前将GUI组件设为不可用
chatJTextArea.setEditable( false );
sendJButton.setEnabled( false );
clearJButton.setEnabled( false );
//在端口6789建立监听socket,并在historyJTextArea中显示提示语
ServerSocket welcomeSocket=new ServerSocket( 6789 );
historyJTextArea.append( "等待连接..."+"\n" );
//当监听socket监听到有客户端连接后建立一个新的socket用于传送数据
//并在historyJTextArea中显示提示语
Socket connectionSocket=welcomeSocket.accept( );
historyJTextArea.append( "连接已建立,端口:"+connectionSocket.getLocalPort()+"\n" );
historyJTextArea.append( "主机信息:"+connectionSocket.getInetAddress().getLocalHost()+"\n" );
historyJTextArea.append( "socket信息:"+connectionSocket+"\n" );
//恢复GUI组件的可用性
chatJTextArea.setEditable( true );
sendJButton.setEnabled( true );
clearJButton.setEnabled( true );
//建立变量fromClient用于存放从客户端接受到的数据
String fromClient;
//通过一个while循环实现连续的接收功能
while( true )
{
//从connectionSocket中取得输出流,并构造PrintWriter对象
OutputStream output=connectionSocket.getOutputStream();
outToClient=new PrintWriter(output,true);
//建立BufferedReader存放从socket中取得的输入流
BufferedReader inFromClient=new BufferedReader(
new InputStreamReader(
connectionSocket.getInputStream( ) ) );
//从输入流缓存区中读取数据存入变量fromClient中
fromClient=inFromClient.readLine( );
if( fromClient!=null)
//在historyJTextArea中显示接收到的数据
{
historyJTextArea.append("Client:"+"\n"+fromClient+"\n");
}
}//end while
}//end try
catch( IOException e )
{
//发生异常时出现提示
JOptionPane.showMessageDialog( null,
e,"发生异常!",
JOptionPane.ERROR_MESSAGE );
}//end catch
finally
{
}//end finally
}
//建立方法sendJButtonActionPerformed用于响应sendJButton的动作
private void sendJButtonActionPerformed( ActionEvent event )
{
//建立变量words存放用户在chatJTextArea里输入的数据
String words=chatJTextArea.getText();
//执行判断,如果用户没有输入数据则提示用户输入数据,如果输入了数据就将其发送到socket中
if( words.equals( "" ) )
{
JOptionPane.showMessageDialog( null,
"输入不能为空,请输入有效字符","输入有误",
JOptionPane.INFORMATION_MESSAGE );
}
else
{
//利用DateFormat的方法getDateTimeInstance取得一个DateFormat对象
//将其存放到time中
DateFormat time=DateFormat.getDateTimeInstance();
//利用new Date()返回当前系统时间,并用time的format方法将其转化为String
//用变量str存放格式化后的要发送的数据
String str=" "+words+"\t"+"("+time.format( new Date() )+")";
try
{
//将str字符串内容发送Client端
outToClient.println(str);
outToClient.flush();
}
catch(Exception e)
{
JOptionPane.showMessageDialog( null,
e,"发生异常!",
JOptionPane.ERROR_MESSAGE );
}
finally
{
}//end finally
sendJButton.requestFocusInWindow();
historyJTextArea.append( "Server:"+"\n"+str+"\n" );
chatJTextArea.setText( "" );
}
}//end method sendJButtonActionPerformed
没有合适的资源?快使用搜索试试~ 我知道了~
TCP.rar_服务器监听
共3个文件
java:2个
txt:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 8 浏览量
2022-09-19
14:25:39
上传
评论
收藏 3KB RAR 举报
温馨提示
服务器和客户机端,即建立所谓的客户机/服务器编程模式。在服务器端必须先建立一个ServerSocket对象,然后等待客户机端的访问。而在客户机端,则是建立一个Socket对象直接跟服务器端连接,如果连接建立成功,则服务器端便会产生一个Socket对象,然后我们就可以利用这个Socket对象跟客户机端的Socket对象沟通了。此时在服务器和客户机之间建立了一条可靠连接,客户机和服务器可以在这条连接上可靠的传送数据。客户机发出请求,服务器监听来自客户机的请求,并为客户机提供相应的服务。
资源推荐
资源详情
资源评论
收起资源包目录
TCP.rar (3个子文件)
TCPServer.java 3KB
TCPClient.java 3KB
www.pudn.com.txt 218B
共 3 条
- 1
资源评论
小贝德罗
- 粉丝: 70
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功