server服务端程序如下
import java.io.*;
import java.net.*;
public class severa
{
static public void main(String args[]) throws IOException
{
ServerSocket sersoc=null;
Socket soc=null;
DataInputStream in =null;
PrintStream out=null;
InetAddress clientIP=null;
String str=null;
try{
sersoc=new ServerSocket(8000);
soc=sersoc.accept();
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
clientIP=soc.getInetAddress();
System.out.println("clent's IP address"+clientIP);
out.println("welcome......");
str=in.readLine();
while(!str.equals("quit"))
{
System.out.println("client said:"+str);
str=in.readLine();
}
//System.out.println(str);
System.out.println("client want to leave");
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
finally
{
in.close();
out.close();
soc.close();
sersoc.close();
System.exit(0);
}
}
}
客户端程序如下
import java.net.*;
import java.io.*;
public class client
{
static public void main(String args[]) throws IOException
{
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
DataInputStream sysin=null;
String strin=null;
String strout=null;
InetAddress name;
try
{
//args[0]="local host";
name=InetAddress.getByName(null);
soc=new Socket(name,8000);//name为服务器名字
System.out.println("connecting to the server ");
//System.out.println("connecting to the server ");
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
strin=in.readLine();
System.out.println("server said:"+strin);
sysin=new DataInputStream(System.in);
strout=sysin.readLine();
while(!strout.equals("quit"));
{
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
finally{
in.close();
out.close();
sysin.close();
soc.close();
System.exit(0);
}
}
}
请问各位大侠 我的程序怎么不能相互通信呀?但能运行,运行完事就没反映了,你往里面输入数据服务端根本得不到
import java.io.*;
import java.net.*;
public class severa
{
static public void main(String args[]) throws IOException
{
ServerSocket sersoc=null;
Socket soc=null;
DataInputStream in =null;
PrintStream out=null;
InetAddress clientIP=null;
String str=null;
try{
sersoc=new ServerSocket(8000);
soc=sersoc.accept();
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
clientIP=soc.getInetAddress();
System.out.println("clent's IP address"+clientIP);
out.println("welcome......");
str=in.readLine();
while(!str.equals("quit"))
{
System.out.println("client said:"+str);
str=in.readLine();
}
//System.out.println(str);
System.out.println("client want to leave");
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
finally
{
in.close();
out.close();
soc.close();
sersoc.close();
System.exit(0);
}
}
}
客户端程序如下
import java.net.*;
import java.io.*;
public class client
{
static public void main(String args[]) throws IOException
{
Socket soc=null;
DataInputStream in=null;
PrintStream out=null;
DataInputStream sysin=null;
String strin=null;
String strout=null;
InetAddress name;
try
{
//args[0]="local host";
name=InetAddress.getByName(null);
soc=new Socket(name,8000);//name为服务器名字
System.out.println("connecting to the server ");
//System.out.println("connecting to the server ");
in=new DataInputStream(soc.getInputStream());
out=new PrintStream(soc.getOutputStream());
strin=in.readLine();
System.out.println("server said:"+strin);
sysin=new DataInputStream(System.in);
strout=sysin.readLine();
while(!strout.equals("quit"));
{
out.println(strout);
strout=sysin.readLine();
}
out.println(strout);
}
catch(Exception e)
{
System.out.println("Error:"+e);
}
finally{
in.close();
out.close();
sysin.close();
soc.close();
System.exit(0);
}
}
}
请问各位大侠 我的程序怎么不能相互通信呀?但能运行,运行完事就没反映了,你往里面输入数据服务端根本得不到
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货