Java源码:Socket编程
2008-04-09 03:59:56来源:互联网 阅读 ()
本文出自:www.chinajavaworld.com 作者: jdeveloper
socket program example!
In this program, I develop an socket server running at port 5000.
Then I develop a Application client and a Applet Client program to
connect to the server and receive a message.This is very simple but
if import usage. We can alse transfer an serializable object in two
application by the same method.If you want to know more let me know
down load source
1. Server program source
// Server.java
/**
* This a Socket Server program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import java.net.*;
import java.io.*;
public class Server
{
ServerSocket server;
DataOutputStream output;
Socket socket;
public Server (){
try{
// create a server on port 5000
server=new ServerSocket(5000);
// display interactive informaion
System.out.println("Server created.");
System.out.println("waiting for client to connect on...");
// waiting for client to connect on...
socket = server.accept();
// client connected
System.out.println("client connected.\nShutdown!");
output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("Welcome to Server.Bye!");
output.close();
server.close();
}
catch(SocketException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
catch(IOException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
}
public static void main(String args[]){
Server game=new Server();
}
}
2. Application Client program source
// Client.java
/**
* This a Socket Client program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: java Client servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream());
// read information from server
String info;
info = input.readUTF();
System.out.println(info);
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}
}
3. Applet Client program source
// AppletClient.java
/**
* This a Socket Applet Client program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class AppletClient extends Applet {
String info="";
public void init(){
try{
String connectto = getCodeBase().getHost();
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream());
// read information from server
info = input.readUTF();
System.out.println(info);
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
info = e.toString();
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
info = e.toString();
}
}
public void paint(Graphics g){
g.drawString(info,20,20);
}
}
Html File AppletClient.html :
<html>
<body>
<hr>
<applet
code=AppletClient.class
width = 200
height = 200
>
</applet>
<hr>
</body>
</html>
4 steps to run the application
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash