基于TCP协议的socket编程基础程序

正在学计算机网络,照着书上的例子,参考了网上的一段代码,写了这个

在线程那段好像有点问题,运行是会有错误,其它还算正常

SSServer.class

 

import  java.io.IOException;
import  java.net.ServerSocket;
import  java.net.Socket;
public   class  SSServer  {

    
/**
     * 
@param args
     * 
@throws IOException 
     
*/

    
public static void main(String[] args) throws IOException 
    
{
        System.out.println (
"Server starting... ");
        ServerSocket server 
= new ServerSocket (10000);
        
while (true)
        
{
            Socket s 
= server.accept ();
            System.out.println (
"Accepting Connection... ");
            
new ServerThread (s).start ();
        }

    }

}

 ServerThread.class

import  java.io. * ;
import  java.net. * ;
import  java.util. * ;
public   class  ServerThread  extends  Thread 
{
    
private Socket s;
    ServerThread (Socket s)
    
{
      
this.s = s;
    }


    
public void run ()
    
{
       BufferedReader br 
= null;
       PrintWriter pw 
= null;
       
try
       
{
           InputStreamReader isr;
           isr 
= new InputStreamReader (s.getInputStream ());
           br 
= new BufferedReader (isr);
           pw 
= new PrintWriter (s.getOutputStream (), true);
           Calendar c 
= Calendar.getInstance ();
       
       
do
       
{
           String cmd 
= br.readLine ();
           
if (cmd == nullbreak;
           cmd 
= cmd.toUpperCase ();
           
if (cmd.startsWith ("BYE"))break;
           
if (cmd.startsWith ("DATE"|| cmd.startsWith ("TIME"))
           pw.println (c.getTime ().toString ());
           
if (cmd.startsWith ("DOM"))
           pw.println (
"" + c.get (Calendar.DAY_OF_MONTH));
           
if (cmd.startsWith ("DOW"))
            
switch (c.get (Calendar.DAY_OF_WEEK))
            
{
              
case Calendar.SUNDAY : pw.println ("SUNDAY");
               
break;

              
case Calendar.MONDAY : pw.println ("MONDAY");
               
break;

              
case Calendar.TUESDAY : pw.println ("TUESDAY");
               
break;

              
case Calendar.WEDNESDAY: pw.println ("WEDNESDAY");
               
break;

              
case Calendar.THURSDAY : pw.println ("THURSDAY");
               
break;

              
case Calendar.FRIDAY : pw.println ("FRIDAY");
               
break;

              
case Calendar.SATURDAY : pw.println ("SATURDAY");
              
            }

           
if (cmd.startsWith ("DOY"))
            pw.println (
"" + c.get (Calendar.DAY_OF_YEAR));
           
if (cmd.startsWith ("PAUSE"))
           
            
try
           
{
              Thread.sleep (
3000);
            }

            
catch (InterruptedException e){}
           
            
       }

            
while (true);
       }

       
            
              
catch (IOException e)
              
{
                System.out.println(e.toString ());
              }

              
finally
              
{
                System.out.println (
"Closing Connection... ");

                
try
                
{
                  
if (br != null)br.close ();

                  
if (pw != null)pw.close ();

                  
if (s!= null) s.close();
                }


                
catch (IOException e){}

              }

      }

}

 

SSclient.class

import  java.io. * ;
import  java.net. * ;
public   class  SSClient  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) 
    
{
        String host 
= "localhost";
        
if (args.length == 1)host = args [0];

            BufferedReader br 
= null;
            PrintWriter pw 
= null;
            Socket s 
= null;
        
try
        
{
            s 
= new Socket (host, 10000);
            InputStreamReader isr;
            isr 
= new InputStreamReader (s.getInputStream ());
            br 
= new BufferedReader (isr);
            pw 
= new PrintWriter (s.getOutputStream (), true);
            pw.println (
"DATE");
            pw.println (
"DATE");
            pw.println (
"PAUSE");
            pw.println (
"DOW");
            System.out.println (br.readLine ());
            System.out.println (br.readLine ());
            pw.println (
"DOY");
            System.out.println (br.readLine ());
        }

        
catch (IOException e)
        
{
          System.out.println (e.toString ());
        }

        
finally
        
{
          
try
          
{
            
if (br != null) br.close();
             
if (pw != null) pw.close();
             
if (s != null)    s.close();
          }

          
catch (IOException e ){}
          
        }


    }


}
这样的三段代码,可以实现socket的互通信息,供大家参考!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值