<%@ page contentType="text/html; charset=gb2312"%> Java socket 入门编程实例
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
Java socket 入门编程实例
     发布者: 发布时间:2006-10-08

这是一个C/S之间通信的例子,在JDK1.4下测试通过.
//服务器端源程序tcpserver.java

import java.io.*;
import java.net.*;
public class tcpserver 
{
    public static void main(String[] args) throws  IOException
    {
        ServerSocket svrsoc=null;
        Socket soc=null;
        DataInputStream in=null;
        PrintStream out=null;
        InetAddress clientIP=null;
        String str=null;
        try
        {
            svrsoc=new ServerSocket(8000);
            System.out.println("Server start....");
            soc=svrsoc.accept();
            
            in=new DataInputStream(soc.getInputStream());
            out=new PrintStream(soc.getOutputStream());
            clientIP=soc.getInetAddress();
            System.out.println("Client's IP address:"+clientIP);
            out.println("welcome.....");
            str=in.readLine();
            while (!str.equals("quit"))
            {
                System.out.println("Client said:"+str);
                str=in.readLine();
            }
            System.out.println("Client want to leave");
        }
        catch(Exception e)
        {
            System.out.println("error:"+e);
        }
        finally
        {
            in.close();
            out.close();
            soc.close();
            svrsoc.close();
            System.exit(0);
        }
    }
}

//客户端源程序tcpclient.java

import java.io.*;
import java.net.*;
public class tcpclient 
{
    public static void main(String[] args) throws  IOException
    {
        Socket soc=null;
        DataInputStream in=null;
        PrintStream out=null;
        DataInputStream sysin=null;
        String strin=null;
        String strout=null;
        try
        {
            soc=new Socket(args[0],8000);
            System.out.println("Connecting to the Server");
            in=new DataInputStream(soc.getInputStream());
            out=new PrintStream(soc.getOutputStream());
            strin=in.readLine();
            System.out.println("Server said:"+strin);
            sysin=new DataInputStream(System.in);
            strout=sysin.readLine();
            while (!strout.equals("quit"))
            {
                out.println(strout);
                strout=sysin.readLine();
            }
            out.println(strout);
        }
        catch(Exception e)
        {
            System.out.println("error:"+e);
        }
        finally
        {
            in.close();
            out.close();
            soc.close();
            sysin.close();
            System.exit(0);
        }
    }
}

(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
实战体会Java的多线程编程
利用Java实现zip压缩/解压缩
javabeans教程
学习java需要知道的一些问题-JSP教程,Java技巧及代
Servlet和JSP迈上新台阶
JSP/Servlet/JSF:标签库的深入研究
 
最近评论:
        
你曾悄悄的来过!
wow gold,wow gold,wow gold,ffxi gil max(3133)
        
冰封的往事!
wow power leveling,wow gold,wow power leveling,wow gold max(3904)
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号