我的eclipse里头写完代码了ServerSocket可以运行但是Socket不能运行是怎么回事。。还是我的操作不对的过
展开全部
不知道你像要问什么!!!
socket是tcp/ip协议中tcp/udp层的实现。你服务器端,正常是new 一个ServerSocket(端口号),该socket就会监听该端口的连接信息。客户端通过ip和port连接服务套接字,这时服务器就可以和客户端建立socket连接。我给你个简单代码看看。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import com.house365.middleware.client.exception.ClientException;
/**
*
* @author Rick
* @Date 2011-9-14
* @comment
*/
public class Test {
public static void startServer(){
new Thread(){
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("server start...");
Socket client;
BufferedReader in ;
String line;
while( (client = serverSocket.accept())!= null){
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public static void sendRequest(String s) throws UnknownHostException, IOException{
Socket client = new Socket("127.0.0.1",7777);
PrintWriter out = new PrintWriter(client.getOutputStream());
out.print(s);
out.flush();
out.close();
client.close();
}
/**
* @param args
* @throws ClientException
* @throws IOException
*/
public static void main(String[] args) throws ClientException, IOException {
//启动服务,服务将会一直在监听
startServer();
//启动一个客户端,发送一个消息
sendRequest("Hello,this is my first socket application");
//启动一个客户端,发送一个消息
sendRequest("Hello,I am Rick");
}
}
socket是tcp/ip协议中tcp/udp层的实现。你服务器端,正常是new 一个ServerSocket(端口号),该socket就会监听该端口的连接信息。客户端通过ip和port连接服务套接字,这时服务器就可以和客户端建立socket连接。我给你个简单代码看看。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import com.house365.middleware.client.exception.ClientException;
/**
*
* @author Rick
* @Date 2011-9-14
* @comment
*/
public class Test {
public static void startServer(){
new Thread(){
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("server start...");
Socket client;
BufferedReader in ;
String line;
while( (client = serverSocket.accept())!= null){
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while((line = in.readLine()) != null){
System.out.println(line);
}
in.close();
client.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public static void sendRequest(String s) throws UnknownHostException, IOException{
Socket client = new Socket("127.0.0.1",7777);
PrintWriter out = new PrintWriter(client.getOutputStream());
out.print(s);
out.flush();
out.close();
client.close();
}
/**
* @param args
* @throws ClientException
* @throws IOException
*/
public static void main(String[] args) throws ClientException, IOException {
//启动服务,服务将会一直在监听
startServer();
//启动一个客户端,发送一个消息
sendRequest("Hello,this is my first socket application");
//启动一个客户端,发送一个消息
sendRequest("Hello,I am Rick");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询