当前位置: 技术问答>java相关
关于socket例子.
来源: 互联网 发布时间:2015-08-01
本文导语: 我想实现书中的两个例子可是。。。 请不要烦帮忙改改,如果有socket方面的例子谢谢给一个!!! 程序: import java.io.*; import java.net.*; public class SimpleClient extends Object implements Runnable//, ReadCallback { pro...
我想实现书中的两个例子可是。。。
请不要烦帮忙改改,如果有socket方面的例子谢谢给一个!!!
程序:
import java.io.*;
import java.net.*;
public class SimpleClient extends Object implements Runnable//, ReadCallback
{
protected Socket serverSock;
protected DataOutputStream outStream;
protected Thread clientThread;
protected ReadThread reader;
public SimpleClient(String hostName, int portNumber)
throws IOException
{
serverSock = new Socket(hostName, portNumber);
outStream = new DataOutputStream(serverSock.getOutputStream());
reader = new ReadThread(this.serverSock);
reader.start();
}
public void start()
{
clientThread = new Thread(this);
clientThread.start();
}
public void stop()
{
clientThread.stop();
clientThread = null;
}
public synchronized void sendString(String str)
throws IOException
{
System.out.println("Sending string:" +str);
outStream.writeUTF(str);
}
public void run()
{
while(true)
{
try{
sendString("Hello There!");
Thread.sleep(2000);
}catch(Exception oops) {
oops.printStackTrace();
disconnect();
stop();
}
}
}
public void disconnect()
{
try{
reader.closeConnection();
}catch(Exception badClose){
}
}
public synchronized void dataReady(String str)
{
System.out.println("Got incoming string:"+ str);
}
public static void main(String[] args)
{
try{
SimpleClient client = new SimpleClient("localhost", 4331);
client.start();
}catch(Exception cantStart) {
System.out.println("Got error");
cantStart.printStackTrace();
}
}
}
//////////////////////////////////////////////////
错误:
C:j2sdk1.4.0_01bin>javac SimpleClient.java
SimpleClient.java:9: cannot resolve symbol
symbol : class ReadThread
location: class SimpleClient
protected ReadThread reader;
^
SimpleClient.java:16: cannot resolve symbol
symbol : class ReadThread
location: class SimpleClient
reader = new ReadThread(this.serverSock);
^
Note: SimpleClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors
/////////////////////////////////////////服务器/////////////////
程序:
import java.io.*;
import java.net.*;
public class ServerConn extends Object implements ReadCallback
{
protected SimpleServer server;
protected Socket clientSock;
protected ReadThread reader;
protected DataOutputStream outStream;
public ServerConn(SimpleServer server, Socket clientSock)
throws IOException
{
this.server = server;
this.clientSock = clientSock;
outStream = new DataOutputStream(clientSock.getOutputStream());
reader = new ReadThread(this, clientSock);
reader.start();
}
public synchronized void dataReady(String str)
{
if(str == null)
{
disconnect();
return;
}
try{
outStream.writeUTF(server.processString(str));
}catch(Exception writeError){
writeError.printStackTrace();
disconnect();
return;
}
}
public synchronized void disconnect()
{
try{
reader.closeConnection();
}catch(Exception cantclose){
reader.stop();
}
}
}
//////////////////////////////////////////
错误:
public class ServerConn extends Object implements ReadCallback
^
ServerConn.java:6: cannot resolve symbol
symbol : class SimpleServer
location: class ServerConn
protected SimpleServer server;
^
ServerConn.java:8: cannot resolve symbol
symbol : class ReadThread
location: class ServerConn
protected ReadThread reader;
^
ServerConn.java:11: cannot resolve symbol
symbol : class SimpleServer
location: class ServerConn
public ServerConn(SimpleServer server, Socket clientSock)
^
ServerConn.java:17: cannot resolve symbol
symbol : class ReadThread
location: class ServerConn
reader = new ReadThread(this, clientSock);
^
5 errors
请不要烦帮忙改改,如果有socket方面的例子谢谢给一个!!!
程序:
import java.io.*;
import java.net.*;
public class SimpleClient extends Object implements Runnable//, ReadCallback
{
protected Socket serverSock;
protected DataOutputStream outStream;
protected Thread clientThread;
protected ReadThread reader;
public SimpleClient(String hostName, int portNumber)
throws IOException
{
serverSock = new Socket(hostName, portNumber);
outStream = new DataOutputStream(serverSock.getOutputStream());
reader = new ReadThread(this.serverSock);
reader.start();
}
public void start()
{
clientThread = new Thread(this);
clientThread.start();
}
public void stop()
{
clientThread.stop();
clientThread = null;
}
public synchronized void sendString(String str)
throws IOException
{
System.out.println("Sending string:" +str);
outStream.writeUTF(str);
}
public void run()
{
while(true)
{
try{
sendString("Hello There!");
Thread.sleep(2000);
}catch(Exception oops) {
oops.printStackTrace();
disconnect();
stop();
}
}
}
public void disconnect()
{
try{
reader.closeConnection();
}catch(Exception badClose){
}
}
public synchronized void dataReady(String str)
{
System.out.println("Got incoming string:"+ str);
}
public static void main(String[] args)
{
try{
SimpleClient client = new SimpleClient("localhost", 4331);
client.start();
}catch(Exception cantStart) {
System.out.println("Got error");
cantStart.printStackTrace();
}
}
}
//////////////////////////////////////////////////
错误:
C:j2sdk1.4.0_01bin>javac SimpleClient.java
SimpleClient.java:9: cannot resolve symbol
symbol : class ReadThread
location: class SimpleClient
protected ReadThread reader;
^
SimpleClient.java:16: cannot resolve symbol
symbol : class ReadThread
location: class SimpleClient
reader = new ReadThread(this.serverSock);
^
Note: SimpleClient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
2 errors
/////////////////////////////////////////服务器/////////////////
程序:
import java.io.*;
import java.net.*;
public class ServerConn extends Object implements ReadCallback
{
protected SimpleServer server;
protected Socket clientSock;
protected ReadThread reader;
protected DataOutputStream outStream;
public ServerConn(SimpleServer server, Socket clientSock)
throws IOException
{
this.server = server;
this.clientSock = clientSock;
outStream = new DataOutputStream(clientSock.getOutputStream());
reader = new ReadThread(this, clientSock);
reader.start();
}
public synchronized void dataReady(String str)
{
if(str == null)
{
disconnect();
return;
}
try{
outStream.writeUTF(server.processString(str));
}catch(Exception writeError){
writeError.printStackTrace();
disconnect();
return;
}
}
public synchronized void disconnect()
{
try{
reader.closeConnection();
}catch(Exception cantclose){
reader.stop();
}
}
}
//////////////////////////////////////////
错误:
public class ServerConn extends Object implements ReadCallback
^
ServerConn.java:6: cannot resolve symbol
symbol : class SimpleServer
location: class ServerConn
protected SimpleServer server;
^
ServerConn.java:8: cannot resolve symbol
symbol : class ReadThread
location: class ServerConn
protected ReadThread reader;
^
ServerConn.java:11: cannot resolve symbol
symbol : class SimpleServer
location: class ServerConn
public ServerConn(SimpleServer server, Socket clientSock)
^
ServerConn.java:17: cannot resolve symbol
symbol : class ReadThread
location: class ServerConn
reader = new ReadThread(this, clientSock);
^
5 errors
|
你的例程是错的,我根据你的第一个例程进行了完善,虽然不够
完整,但已经可以编译运行了:
//##################################################
// file: SimpleClient.java
//
import java.io.*;
import java.net.*;
public class SimpleClient extends Object implements Runnable //, ReadCallback
{
protected Socket serverSock;
protected DataOutputStream outStream;
protected Thread clientThread;
protected ReadThread reader;
// --------------------------------------------------
class ReadThread extends Thread{
ServerSocket s;
Socket serverSock;
DataInputStream input;
String str;
int portnum;
public ReadThread(int port) throws IOException{
portnum = port;
s = new ServerSocket(portnum);
}
public void closeConnection(){
try {
serverSock.close();
} catch (IOException ex) {
}
return;
}
public void start()
{
try {
serverSock = s.accept();
input = new DataInputStream(serverSock.getInputStream());
} catch (IOException ex) {
System.err.println("Read nothing from client.");
this.stop();
}
while (true){
try {
str = new String(input.readUTF());
} catch (IOException ex) {
System.err.println("Cannot read input:"+ex.toString());
this.stop();
}
System.out.println("Server - "+str);
}
}
}
// ------------------------------------------------------------
public SimpleClient(String hostName, int portNumber)
throws IOException
{
reader = new ReadThread(portNumber);
serverSock = new Socket(InetAddress.getLocalHost(), portNumber);
outStream = new DataOutputStream(serverSock.getOutputStream());
}
public void start()
{
clientThread = new Thread(this);
clientThread.start();
reader.start();
}
public void stop()
{
clientThread.stop();
clientThread = null;
}
public synchronized void sendString(String str)
throws IOException
{
System.out.println("Client - Sending string:" +str);
outStream.writeUTF(str);
}
public void run()
{
while(true)
{
try{
sendString("Hello There!");
Thread.sleep(2000);
}catch(Exception oops) {
oops.printStackTrace();
disconnect();
stop();
}
}
}
public void disconnect()
{
try{
reader.closeConnection();
}catch(Exception badClose){
}
}
public synchronized void dataReady(String str)
{
System.out.println("Got incoming string:"+ str);
}
public static void main(String[] args)
{
try{
SimpleClient client = new SimpleClient("localhost", 2525);
client.start();
} catch(Exception cantStart) {
System.out.println("Got error");
cantStart.printStackTrace();
}
}
}
完整,但已经可以编译运行了:
//##################################################
// file: SimpleClient.java
//
import java.io.*;
import java.net.*;
public class SimpleClient extends Object implements Runnable //, ReadCallback
{
protected Socket serverSock;
protected DataOutputStream outStream;
protected Thread clientThread;
protected ReadThread reader;
// --------------------------------------------------
class ReadThread extends Thread{
ServerSocket s;
Socket serverSock;
DataInputStream input;
String str;
int portnum;
public ReadThread(int port) throws IOException{
portnum = port;
s = new ServerSocket(portnum);
}
public void closeConnection(){
try {
serverSock.close();
} catch (IOException ex) {
}
return;
}
public void start()
{
try {
serverSock = s.accept();
input = new DataInputStream(serverSock.getInputStream());
} catch (IOException ex) {
System.err.println("Read nothing from client.");
this.stop();
}
while (true){
try {
str = new String(input.readUTF());
} catch (IOException ex) {
System.err.println("Cannot read input:"+ex.toString());
this.stop();
}
System.out.println("Server - "+str);
}
}
}
// ------------------------------------------------------------
public SimpleClient(String hostName, int portNumber)
throws IOException
{
reader = new ReadThread(portNumber);
serverSock = new Socket(InetAddress.getLocalHost(), portNumber);
outStream = new DataOutputStream(serverSock.getOutputStream());
}
public void start()
{
clientThread = new Thread(this);
clientThread.start();
reader.start();
}
public void stop()
{
clientThread.stop();
clientThread = null;
}
public synchronized void sendString(String str)
throws IOException
{
System.out.println("Client - Sending string:" +str);
outStream.writeUTF(str);
}
public void run()
{
while(true)
{
try{
sendString("Hello There!");
Thread.sleep(2000);
}catch(Exception oops) {
oops.printStackTrace();
disconnect();
stop();
}
}
}
public void disconnect()
{
try{
reader.closeConnection();
}catch(Exception badClose){
}
}
public synchronized void dataReady(String str)
{
System.out.println("Got incoming string:"+ str);
}
public static void main(String[] args)
{
try{
SimpleClient client = new SimpleClient("localhost", 2525);
client.start();
} catch(Exception cantStart) {
System.out.println("Got error");
cantStart.printStackTrace();
}
}
}
|
你这里缺东西吧
这个应该是个读数据ReadThred()类找不到你这里没有
你在看看书上找一下这个类就可以了
这个应该是个读数据ReadThred()类找不到你这里没有
你在看看书上找一下这个类就可以了
|
ReadThread 是什么啊,好象是没有找到这个类
|
java的socket方面的东西网上很多,还是去下载一些下来看看,再往下走。
|
ReadThread 的代码可能在书的其他地方,你在找找
|
client 和 server 程序放在一起吗?server程序是自动运行的吗?
哈,我是个菜鸟,知道客户端是小程序,可以通过浏览器运行,
可服务器端的是application呀,就放在那里不用管它?
哈,我是个菜鸟,知道客户端是小程序,可以通过浏览器运行,
可服务器端的是application呀,就放在那里不用管它?