当前位置: 技术问答>java相关
客户端 不能发送信息到服务器端?(在SOCKET)请看看下面这段程序有什么错?----客户端的发送信息服务器端收不了!
来源: 互联网 发布时间:2015-01-17
本文导语: //服务器端 ! import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) { ServerSocket s=null; Socket s1; String sendString="WellCome you to connect to me!"; OutputStream sout; DataOutputStream dos; ...
//服务器端 !
import java.net.*;
import java.io.*;
public class SimpleServer
{
public static void main(String args[])
{
ServerSocket s=null;
Socket s1;
String sendString="WellCome you to connect to me!";
OutputStream sout;
DataOutputStream dos;
InputStream sint;
DataInputStream dis;
try
{
s=new ServerSocket(1234);
}
catch(IOException e){}
while(true){
try{
s1=s.accept();
sout=s1.getOutputStream();
dos=new DataOutputStream(sout);
dos.writeUTF(sendString);
sint=s1.getInputStream();
dis=new DataInputStream(sint);
String st=new String(dis.readUTF());
System.out.println(st);//就是这里不能打印出客户端的信息“I am client !");”
sout.close();
s1.close();
}
catch(IOException e){}
}
}
}
---------------------------------------------------------------
客户端:
import java.net.*;
import java.io.*;
public class SimpleClient
{
public static void main(String agrs[]) throws IOException
{
int c;
Socket s1;
InputStream istr;
OutputStream ostr;
DataInputStream distr;
DataOutputStream dostr;
s1=new Socket("ruanwq",1234);
istr=s1.getInputStream();
distr=new DataInputStream(istr);
String st=new String(distr.readUTF());
System.out.println(st);
ostr=s1.getOutputStream();
dostr=new DataOutputStream(ostr);
dostr.writeUTF("I am client !");
ostr.close();
istr.close();
s1.close();
}
}
import java.net.*;
import java.io.*;
public class SimpleServer
{
public static void main(String args[])
{
ServerSocket s=null;
Socket s1;
String sendString="WellCome you to connect to me!";
OutputStream sout;
DataOutputStream dos;
InputStream sint;
DataInputStream dis;
try
{
s=new ServerSocket(1234);
}
catch(IOException e){}
while(true){
try{
s1=s.accept();
sout=s1.getOutputStream();
dos=new DataOutputStream(sout);
dos.writeUTF(sendString);
sint=s1.getInputStream();
dis=new DataInputStream(sint);
String st=new String(dis.readUTF());
System.out.println(st);//就是这里不能打印出客户端的信息“I am client !");”
sout.close();
s1.close();
}
catch(IOException e){}
}
}
}
---------------------------------------------------------------
客户端:
import java.net.*;
import java.io.*;
public class SimpleClient
{
public static void main(String agrs[]) throws IOException
{
int c;
Socket s1;
InputStream istr;
OutputStream ostr;
DataInputStream distr;
DataOutputStream dostr;
s1=new Socket("ruanwq",1234);
istr=s1.getInputStream();
distr=new DataInputStream(istr);
String st=new String(distr.readUTF());
System.out.println(st);
ostr=s1.getOutputStream();
dostr=new DataOutputStream(ostr);
dostr.writeUTF("I am client !");
ostr.close();
istr.close();
s1.close();
}
}
|
可以发的
可能是这一句
s1=new Socket("ruanwq",1234);//你要吧ruanwq改为你的计算机的名字就好了
可能是这一句
s1=new Socket("ruanwq",1234);//你要吧ruanwq改为你的计算机的名字就好了
|
好熟悉的程序,不知道在哪里看到过