Java socket 入门编程实例
2009-11-20 15:36
417 查看
这是一个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);
}
}
}
//服务器端源程序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网络编程基础教程之Socket入门实例
- 初学java socket编程实例代码讲解
- 初学者入门 JAVA 的 XML 编程实例解析
- java网络编程【2】 基于Socket的java网络编程(含实例)
- C#中的socket编程入门实例
- Linux/Unix服务端和客户端Socket编程入门实例(含源码下载)
- JAVA socket编程实例
- 36、Java入门—Socket实例之客户端向服务端数据库上传文件UI版
- Java Socket编程服务器响应客户端实例代码
- Java Socket网络编程初级入门-Java基础-Java-编程开发
- java中的socket编程的入门源码
- Linux/Unix服务端和客户端Socket编程入门实例(含源码下载)
- Linux/Unix服务端和客户端Socket编程入门实例代码
- java网络编程实例1——用socket类写http客户端
- Java Socket编程实例(四)- NIO TCP实践
- JAVA socket编程实例
- Java socket 入门编程实例-Java基础-Java-编程开发
- Java网络编程之Socket入门
- JAVA socket编程实例
- JAVA socket编程实例