Java的网络连接 1:我记得<JAVA高级特性>2中有一篇讲过类似的问题.2:可以,在csdn java区中搜一下.有解答.3:不知是什么意思.能在讲清楚些吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是典型的网络程序设计,Java怎么可能作不到呢?你看一下http://www.java.sun.com/docs/books/tutorial/networking/sockets/index.htmlJava调用C/C++程序,可以用 Runtime.getRuntime().exec ("文件名")而且可以用JNI调用Win32的函数等等,还可以调用COM组件,但是这些就失去了Java的跨平台的特性! 很典型的嘛,建立Socket,建立IO流,就行了,别忘了多线程! 1和2都是JAVA的典型应用,我试过的!给你贴个很简单的C/S的例子启发一下你,希望对你有用。//================Server端=====================import java.net.*;import java.io.*;import java.util.*;public class javaserver extends Thread { protected ServerSocket server; public javaserver(){ try{ server = new ServerSocket(600); }catch(IOException e){ System.out.println("Cannot create serversocket!Error is " + e); System.exit(0); } System.out.println("New serversocket is created!"); this.start(); } public void run() { try { while (true) { Socket client = server.accept(); service ss = new service(client); } }catch(IOException e){ System.out.println("Cannot provide service!"); System.exit(1); } } public static void main(String args[]) { String data; DataInputStream KeyInput; new javaserver(); KeyInput = new DataInputStream(System.in); try { data = KeyInput.readLine(); }catch(IOException e) { return; } if (data.equals("quit")) System.exit(1); } }class service extends Thread { String data; DataInputStream InputS; PrintStream OutputS; Socket client; public service(Socket ClientSocket) { client = ClientSocket; try { InputS = new DataInputStream(client.getInputStream()); OutputS = new PrintStream(client.getOutputStream()); }catch(IOException e){ System.out.println("Cannot connect server!"); return; } this.start(); } public void run() { try { while (true) { data = InputS.readLine(); if (data==null) break; else { OutputS.println(data); System.out.println("From client: " + data); } } }catch(IOException e) { System.out.println("Cannot read data"); } try{ client.close(); }catch(IOException e) { System.out.println("Cannot close socket"); } }} //================Client端================import java.net.*;import java.io.*;import java.util.*;public class javaclient { public static void main(String args[]) { String data; Socket client; DataInputStream InputS; DataInputStream KeyS; PrintStream OutputS; int i = 0; try{ client = new Socket("127.0.0.1",600); InputS = new DataInputStream(client.getInputStream()); OutputS = new PrintStream(client.getOutputStream()); KeyS = new DataInputStream(System.in); }catch(IOException e) { System.out.println("Cannot Connect with server"); return; } try { while(i<5) { data = KeyS.readLine(); OutputS.println(data); System.out.println("Echo From server:" + InputS.readLine()); i++; } }catch(IOException e){ System.out.println("IOException Happended"); } try { System.out.println("Now will end this program"); client.close(); }catch(IOException e){ System.out.println("System cannot close socket"); } }} For 循环问题求解 Java中有关数组排列的问题,求解 求助:jdbc中的问题 求swing的对话框代码 rss与jsp如何结合在一起 JTable的问题 JTree节点之间的距离怎么控制?在线等待 关于字符集的问题,究竟什么是“ISO 8859-1”,还有其它字符集吗?有什么区别? SMTP 发信通过验证了,可没有发出信怎么回事?附程序 谢谢!!! ssl用JAVA编程实现 关于小程序中访问数据库 哪里有java人气好的论坛与文档
你看一下http://www.java.sun.com/docs/books/tutorial/networking/sockets/index.html
Java调用C/C++程序,可以用 Runtime.getRuntime().exec ("文件名")
而且可以用JNI调用Win32的函数等等,还可以调用COM组件,但是这些就失去了Java的
跨平台的特性!
很典型的嘛,建立Socket,建立IO流,就行了,别忘了多线程!
给你贴个很简单的C/S的例子启发一下你,希望对你有用。
//================Server端=====================
import java.net.*;
import java.io.*;
import java.util.*;public class javaserver extends Thread {
protected ServerSocket server;
public javaserver(){
try{
server = new ServerSocket(600);
}catch(IOException e){
System.out.println("Cannot create serversocket!Error is " + e);
System.exit(0);
}
System.out.println("New serversocket is created!");
this.start();
}
public void run() {
try {
while (true) {
Socket client = server.accept();
service ss = new service(client);
}
}catch(IOException e){
System.out.println("Cannot provide service!");
System.exit(1);
}
} public static void main(String args[]) {
String data;
DataInputStream KeyInput;
new javaserver();
KeyInput = new DataInputStream(System.in);
try {
data = KeyInput.readLine();
}catch(IOException e)
{
return;
}
if (data.equals("quit")) System.exit(1);
}
}class service extends Thread {
String data;
DataInputStream InputS;
PrintStream OutputS;
Socket client;
public service(Socket ClientSocket) { client = ClientSocket;
try {
InputS = new DataInputStream(client.getInputStream());
OutputS = new PrintStream(client.getOutputStream());
}catch(IOException e){
System.out.println("Cannot connect server!");
return;
} this.start(); } public void run() {
try {
while (true) {
data = InputS.readLine();
if (data==null) break;
else {
OutputS.println(data);
System.out.println("From client: " + data);
}
}
}catch(IOException e) {
System.out.println("Cannot read data");
}
try{
client.close();
}catch(IOException e) {
System.out.println("Cannot close socket");
}
}
}
//================Client端================
import java.net.*;
import java.io.*;
import java.util.*;public class javaclient {
public static void main(String args[]) {
String data;
Socket client;
DataInputStream InputS;
DataInputStream KeyS;
PrintStream OutputS; int i = 0;
try{
client = new Socket("127.0.0.1",600);
InputS = new DataInputStream(client.getInputStream());
OutputS = new PrintStream(client.getOutputStream());
KeyS = new DataInputStream(System.in);
}catch(IOException e) {
System.out.println("Cannot Connect with server");
return;
}
try {
while(i<5) {
data = KeyS.readLine();
OutputS.println(data);
System.out.println("Echo From server:" + InputS.readLine());
i++;
}
}catch(IOException e){
System.out.println("IOException Happended");
}
try {
System.out.println("Now will end this program");
client.close();
}catch(IOException e){
System.out.println("System cannot close socket");
}
}
}