当前位置: 技术问答>java相关
请教各位一个关于socket的初级问题?
来源: 互联网 发布时间:2014-12-23
本文导语: 我是一个java新手,近来学习socket时遇到了些问题,请各位多多指教 以下两个文件一个是server,一个是client.运行时client不能收到server的信息, 但server可以收到client的信息。 //testClient.java import java.io.*; import java.net.*; ...
我是一个java新手,近来学习socket时遇到了些问题,请各位多多指教
以下两个文件一个是server,一个是client.运行时client不能收到server的信息,
但server可以收到client的信息。
//testClient.java
import java.io.*;
import java.net.*;
public class testClient
{
final static int CONN_PORT = 8089;
public static void main(String args[]) throws UnknownHostException, IOException
{
InetAddress host;
String serverResponse;
DataInputStream inn;
DataOutputStream outt;
BufferedReader in;
PrintWriter out;
String welcome;
Socket clientSocket;
host = InetAddress.getByName("localhost");
clientSocket = new Socket(host, CONN_PORT);
while(true)
{ try
{
if(clientSocket == null)
{
System.exit(10);
}
inn = new DataInputStream(clientSocket.getInputStream());
outt = new DataOutputStream(clientSocket.getOutputStream());
in =new BufferedReader (new InputStreamReader(inn));
out = new PrintWriter (new OutputStreamWriter(outt),true);
System.out.println("cliet is ready");
welcome = in.readLine();
System.out.println("Server reply: " + welcome);
out.println("hello");
System.out.println("Client write: Hello");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
//testServer.java
import java.util.*;
import java.io.*;
import java.net.*;
public class testServer
{
final static int SERVER_PORT = 8089;
public static void main(String args[])
{
String clientRequest;
Server server;
BufferedReader in;
PrintWriter out;
server = new Server(SERVER_PORT);
if (server == null)
{
System.exit(10);
}
in = new BufferedReader(new InputStreamReader(server.in));
out =new PrintWriter(new OutputStreamWriter(server.out));
out.println("Java test ServerSocket: " + new Date());
while(true)
{
try
{
clientRequest = in.readLine();
System.out.println("Client writes: " + clientRequest);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Server
{
private ServerSocket server;
private Socket socket1;
public InputStream in;
public OutputStream out;
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("ServerSocket before accept: " + server);
System.out.println("Java test ServerSocket ver0.1 online!");
//wait for client to connect to server
socket1 = server.accept();
System.out.println("ServerSocket after accept: " + server);
in = socket1.getInputStream();
out = socket1.getOutputStream();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
以下两个文件一个是server,一个是client.运行时client不能收到server的信息,
但server可以收到client的信息。
//testClient.java
import java.io.*;
import java.net.*;
public class testClient
{
final static int CONN_PORT = 8089;
public static void main(String args[]) throws UnknownHostException, IOException
{
InetAddress host;
String serverResponse;
DataInputStream inn;
DataOutputStream outt;
BufferedReader in;
PrintWriter out;
String welcome;
Socket clientSocket;
host = InetAddress.getByName("localhost");
clientSocket = new Socket(host, CONN_PORT);
while(true)
{ try
{
if(clientSocket == null)
{
System.exit(10);
}
inn = new DataInputStream(clientSocket.getInputStream());
outt = new DataOutputStream(clientSocket.getOutputStream());
in =new BufferedReader (new InputStreamReader(inn));
out = new PrintWriter (new OutputStreamWriter(outt),true);
System.out.println("cliet is ready");
welcome = in.readLine();
System.out.println("Server reply: " + welcome);
out.println("hello");
System.out.println("Client write: Hello");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
//testServer.java
import java.util.*;
import java.io.*;
import java.net.*;
public class testServer
{
final static int SERVER_PORT = 8089;
public static void main(String args[])
{
String clientRequest;
Server server;
BufferedReader in;
PrintWriter out;
server = new Server(SERVER_PORT);
if (server == null)
{
System.exit(10);
}
in = new BufferedReader(new InputStreamReader(server.in));
out =new PrintWriter(new OutputStreamWriter(server.out));
out.println("Java test ServerSocket: " + new Date());
while(true)
{
try
{
clientRequest = in.readLine();
System.out.println("Client writes: " + clientRequest);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class Server
{
private ServerSocket server;
private Socket socket1;
public InputStream in;
public OutputStream out;
public Server(int port)
{
try
{
server = new ServerSocket(port);
System.out.println("ServerSocket before accept: " + server);
System.out.println("Java test ServerSocket ver0.1 online!");
//wait for client to connect to server
socket1 = server.accept();
System.out.println("ServerSocket after accept: " + server);
in = socket1.getInputStream();
out = socket1.getOutputStream();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
|
Here is the souce code I write for a time server using socket. Hope it will help you.
// 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