Java Socket编程
来源:互联网 发布:mysql数据库如何启动 编辑:程序博客网 时间:2024/04/29 06:49
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.
Shutdown!");
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
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()); JAVA天堂
// 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 :
code=AppletClient.class
width = 200
height = 200
>
4 steps to run the application
1. javac Server.java
2. javac Client.java
3. java Server
4. java Client localhost(or the computername in which the server is running)
! do the step 3 and 4 in two diffrent computer or Dos Commnand
来自:http://www.javah.net/wangluobiancheng/20070604/2278.html
- Java Socket编程
- Java Socket编程
- Java Socket编程(
- JAVA SOCKET编程
- Java Socket编程
- Java Socket编程
- java socket编程
- Java Socket编程
- Java Socket编程之一
- java socket编程
- Java Socket编程
- Java Socket编程
- Java Socket编程介绍
- Java Socket编程
- Java Socket编程
- java socket编程范例
- Java Socket编程
- java Socket编程基础
- J2EE会话外观模式与值对象
- Windows Vista蓝屏代码含义和解决方案
- Windows内存管理
- 股市十大心里管理法则
- 设计模式的原则
- Java Socket编程
- sizeof终极无惑 转载自: 拼搏之路
- 七条股市赢家法则
- C/C++语言void及void指针深层探索
- 好久不写文章,最近研究 BSP技术!!!!!!!!!!!!
- C#与IronPython脚本进行数据交互示例
- 读x264代码完全结束,哇哈哈哈哈
- XDoclet:EJB开发好帮手
- Vista BitLocker 驱动器加密原理