当前位置: 技术问答>java相关
怎么用java进行socket编程
来源: 互联网 发布时间:2015-05-04
本文导语: 怎么用java进行socket编程, java.net.Socket怎样使用,能举一个简单例子吗 | 试试这个 // The server: import java.net.*; import java.io.*; import java.util.Date; /** * A class send out date infomation through socket. * *...
怎么用java进行socket编程, java.net.Socket怎样使用,能举一个简单例子吗
|
试试这个
// The server:
import java.net.*;
import java.io.*;
import java.util.Date;
/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
public static final int PORT = 1300;
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
Socket connection = null;
while (true) { // using a while (true) loop
try
{
connection = server.accept();
OutputStreamWriter out =
new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "rn");
out.flush();
connection.close();
} catch (IOException ioe) {
System.err.println(ioe);
} finally {
try {
if (connection != null)
connection.close();
} catch (IOException e) {}
}
}
} catch (IOException ee) {
System.err.println(ee);
}
} // end main
} // end DaytimeServer
// The client
import java.net.*;
import java.io.*;
/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
/**
* The port which client and server make connection.
*/
public static final int PORT = 1300;
public static void main(String[] args) {
String hostName;
if (args.length > 0) {
hostName = args[0];
} else {
hostName = "localhost";
}
try {
Socket clientSocket = new Socket(hostName, PORT);
InputStream timeStream = clientSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) {
time.append((char)c);
}
String timeString = time.toString().trim();
System.out.println("It is " + timeString + " at " + hostName);
} catch (UnknownHostException ukhe) {
System.err.println(ukhe);
} catch (IOException ioe) {
System.err.println(ioe);
}
} // end main
} // end DaytimeClient
// The server:
import java.net.*;
import java.io.*;
import java.util.Date;
/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
public static final int PORT = 1300;
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(PORT);
Socket connection = null;
while (true) { // using a while (true) loop
try
{
connection = server.accept();
OutputStreamWriter out =
new OutputStreamWriter(connection.getOutputStream());
Date now = new Date();
out.write(now.toString() + "rn");
out.flush();
connection.close();
} catch (IOException ioe) {
System.err.println(ioe);
} finally {
try {
if (connection != null)
connection.close();
} catch (IOException e) {}
}
}
} catch (IOException ee) {
System.err.println(ee);
}
} // end main
} // end DaytimeServer
// The client
import java.net.*;
import java.io.*;
/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
/**
* The port which client and server make connection.
*/
public static final int PORT = 1300;
public static void main(String[] args) {
String hostName;
if (args.length > 0) {
hostName = args[0];
} else {
hostName = "localhost";
}
try {
Socket clientSocket = new Socket(hostName, PORT);
InputStream timeStream = clientSocket.getInputStream();
StringBuffer time = new StringBuffer();
int c;
while ((c = timeStream.read()) != -1) {
time.append((char)c);
}
String timeString = time.toString().trim();
System.out.println("It is " + timeString + " at " + hostName);
} catch (UnknownHostException ukhe) {
System.err.println(ukhe);
} catch (IOException ioe) {
System.err.println(ioe);
}
} // end main
} // end DaytimeClient
|
http://61.144.28.245/hjc/web/上有个好例子。
|
ServerAddr is InetAddress
cPort is port(int)
then is ok
cPort is port(int)
then is ok
|
IBM的DW上有一篇很好的教程,是讲socket的,你去看看:
http://www-900.ibm.com/developerWorks/cnedu.nsf/java-onlinecourse-bytitle/57BECD39CCDC7AF248256B180022C17D?OpenDocument
http://www-900.ibm.com/developerWorks/cnedu.nsf/java-onlinecourse-bytitle/57BECD39CCDC7AF248256B180022C17D?OpenDocument