国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Java socket 入門(mén)編程實(shí)例

2019-11-18 10:40:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這是一個(gè)C/S之間通信的例子,在JDK1.4下測(cè)試通過(guò).
//服務(wù)器端源程序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);
}
}
}

//客戶(hù)端源程序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);
}
}
}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉田县| 上林县| 汉中市| 灯塔市| 奉节县| 平乐县| 阳春市| 任丘市| 琼海市| 台前县| 营口市| 平邑县| 共和县| 韩城市| 社旗县| 建湖县| 荔浦县| 霍林郭勒市| 吉隆县| 沈丘县| 会泽县| 漳州市| 北川| 西昌市| 衡南县| 汉源县| 甘南县| 洛南县| 涞源县| 庐江县| 项城市| 湘潭县| 宁远县| 永寿县| 麻城市| 深水埗区| 桂东县| 成都市| 重庆市| 乌兰察布市| 松桃|