请问各位前辈,这连个使用了套接字的程序为何会连接不上呢?代码完全没有错误啊!请大家指点一下!
import java.io.*;
import java.net.*;
public class TestServer {
public static void main(String args[])
{
ServerSocket server = null;
Socket socket;
String s=null;
DataOutputStream out =null;
DataInputStream in = null;
try{
server = new ServerSocket(4141);
}
catch(IOException ee){
System.out.println("没有连上"+ee);
}
try{
System.out.println("waiting the client...");
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
while(true){
s = in.readUTF();
int m = Integer.parseInt(s);
out.writeUTF("hello,i am the server");
out.writeUTF("the num*2 is:"+m*2);
System.out.println("server receive:"+m);
Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("lose conection!");
}
}}
-----------------------------------------
Client:
import java.io.*;
import java.net.*;
public class TestClient {
public static void main(String args[])
{
String s=null;
Socket socket;
DataInputStream in = null;
DataOutputStream out = null;
try{
socket = new Socket("127.0.0.1",4141);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
for(int i =1 ; i<100; i=i+2){
out.writeUTF(" "+i);
s=in.readUTF();
System.out.println("client have received:"+s);
Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("client lose conection!");
}
}}
import java.io.*;
import java.net.*;
public class TestServer {
public static void main(String args[])
{
ServerSocket server = null;
Socket socket;
String s=null;
DataOutputStream out =null;
DataInputStream in = null;
try{
server = new ServerSocket(4141);
}
catch(IOException ee){
System.out.println("没有连上"+ee);
}
try{
System.out.println("waiting the client...");
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
while(true){
s = in.readUTF();
int m = Integer.parseInt(s);
out.writeUTF("hello,i am the server");
out.writeUTF("the num*2 is:"+m*2);
System.out.println("server receive:"+m);
Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("lose conection!");
}
}}
-----------------------------------------
Client:
import java.io.*;
import java.net.*;
public class TestClient {
public static void main(String args[])
{
String s=null;
Socket socket;
DataInputStream in = null;
DataOutputStream out = null;
try{
socket = new Socket("127.0.0.1",4141);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
for(int i =1 ; i<100; i=i+2){
out.writeUTF(" "+i);
s=in.readUTF();
System.out.println("client have received:"+s);
Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("client lose conection!");
}
}}
public class TestServer {
public static void main(String args[])
{
ServerSocket server = null;
Socket socket;
String s=null;
DataOutputStream out =null;
DataInputStream in = null;
try{
server = new ServerSocket(4141);
}
catch(IOException ee){
System.out.println("没有连上"+ee);
}
try{
System.out.println("waiting the client...");
socket = server.accept();
out = new DataOutputStream(socket.getOutputStream());
//in = new DataInputStream(socket.getInputStream());
while(true){
//s = in.readUTF();
//int m = Integer.parseInt(s);
//发送数据
out.writeUTF("hello,i am the server");
//out.writeUTF("the num*2 is:"+m*2);
//System.out.println("server receive:"+m);
//Thread不是这样写的 具体逻辑发送和接收分开 网上搜很多
//Thread.sleep(500);
}
}
catch(Exception e){
System.out.println("lose conection!");
}
} }
你用ipconfig查一下自己机器的IP地址,用得到地址,换掉socket = new Socket("127.0.0.1",4141);语句中的,127.0.0.1。那你就用这个办法试试吧。。
先保证s!=null就行了
Server中:
int m = Integer.parseInt(s); 该句抛出了解析格式错误的异常,导致服务器退出,从而客户端失去了连接。原因:客户端向服务器端发送的数据是" " + i,里面含有一个空格,这种字符串不能被解析成int形式,请把字符串里边的空格去掉。建议:
以后编写异常处理的时候尽量把异常内容打印出来,便于查找错误。