<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>这是一个C/S之间通信的例子,在JDK1.4下测试通过.
//服务器端源程序tcpserver.java
//服务器端源程序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);
}
}
}
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);
}
}
}
本文提供了一个基于JDK1.4的C/S架构通信示例,包括服务器端(tcpserver.java)与客户端(tcpclient.java)的Java实现。服务器监听8000端口,并与客户端进行文本交互直至客户端输入'quit'结束会话。
16万+

被折叠的 条评论
为什么被折叠?



