Let's Go!

Java socket 入门编程实例----- 两个例子都有问题,仅作参考

上一篇 / 下一篇  2009-04-27 15:48:17 / 个人分类:JAVA学习&编程相关

 

 

例子是学习编程的法宝。你在学习java Socket 吗?看看下

面的这个例子吧!
实现Client端功能的ClientApp.java原文件:

import java.net.*;
import java.io.*;
import java.lang.*;

public class ClientApp
{
public static void main(String args[])
{
try
{
//创建通讯并且和主机Rock连接
Socket cSocket=new Socket("192.168.100.188",8018);
//打开这个Socket的输入/输出流
OutputStream s=cSocket.getOutputStream();
DataInputStream is=new DataInputStream

(cSocket.getInputStream());

int c;
boolean flag=true;

String responseline;

while(flag)
{
//从标准输入输出接受字符并且写如系统
while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c==''\n'')
{
os.flush();
//将程序阻塞,直到回答信息被收到后将他们在标准输出上

显示出来
responseline=is.readLine();
System.out.println("Message is:"+responseline);
}
}
}
os.close();
is.close();
cSocket.close();

}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}  


   实现Server端功能的ServerApp.java原文件:

import java.net.*;
import java.io.*;

public class ServerApp
{
public static void main(String args[])
{
try
{
boolean flag=true;
Socket clientSocket=null;
String inputLine;
int c;

ServerSocket sSocket=new ServerSocket(8018);
System.out.println("Server listen

on:"+sSocket.getLocalPort());

while(flag)
{
clientSocket=sSocket.accept();
DataInputStream is= new DataInputStream(new

BufferedInputStream(clientSocket.getInputStream()));
OutputStream s=clientSocket.getOutputStream();

while((inputLine=is.readLine())!=null)
{
//当客户端输入stop的时候服务器程序运行终止!
if(inputLine.equals("stop"))
{
flag=false;
break;
}
else
{
System.out.println(inputLine);

while((c=System.in.read())!=-1)
{
os.write((byte)c);
if(c==''\n'')
{
os.flush(); //将信息发送到客户端
break;
}
}
}


}
is.close();
os.close();
clientSocket.close();

}
sSocket.close();
}
catch(Exception e)
{
System.out.println("Exception :"+ e.getMessage());
}
}
}    

 

 

 

 

===========================================================

Java socket编程实例
2006-12-13 01:59

这是一个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 ut=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 ut=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);
}
}
}


TAG:

 

评分:0

我来说两句

Open Toolbar