java客户端与服务器
怎么在窗口中输入用户名与密码,然后登入,经客户端必须以,隔开。例如:用户名:123,密码:123客户端接受为123,123并在服务器中输出用户名与密码,这时又是12312...
怎么在窗口中输入用户名与密码,然后登入,经客户端必须以,隔开。例如:用户名:123,密码:123客户端接受为123,123
并在服务器中输出用户名与密码,这时又是
123
123
在此先谢谢各位了。
不太明白啊。能不能加上注释啊 展开
并在服务器中输出用户名与密码,这时又是
123
123
在此先谢谢各位了。
不太明白啊。能不能加上注释啊 展开
2个回答
2009-06-05
展开全部
先启动服务器,再启动客户端
//Server.java
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
int port = 12345;
ServerSocket server = new ServerSocket(port);
System.out.println("服务器就绪,等待用户登录:");
Socket so = server.accept();
byte[] buff = new byte[1024];
int read = so.getInputStream().read(buff);
String str = new String(buff,0,read);
String[] split = str.split(",");
for(int i=0; i<split.length; i++)
System.out.println(split[i]);
so.close();
server.close();
}
}
//Client.java
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
int port = 12345;
System.out.print("在此输入用户名和密码,并用','分开:");
byte[] buff = new byte[1024];
int read = System.in.read(buff);
String str = new String(buff,0,read);
Socket so = new Socket("localhost",port);
so.getOutputStream().write(str.getBytes());
so.getOutputStream().flush();
so.getOutputStream().close();
so.close();
}
}
//Server.java
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
int port = 12345;
ServerSocket server = new ServerSocket(port);
System.out.println("服务器就绪,等待用户登录:");
Socket so = server.accept();
byte[] buff = new byte[1024];
int read = so.getInputStream().read(buff);
String str = new String(buff,0,read);
String[] split = str.split(",");
for(int i=0; i<split.length; i++)
System.out.println(split[i]);
so.close();
server.close();
}
}
//Client.java
import java.net.Socket;
public class Client {
public static void main(String[] args) throws Exception {
int port = 12345;
System.out.print("在此输入用户名和密码,并用','分开:");
byte[] buff = new byte[1024];
int read = System.in.read(buff);
String str = new String(buff,0,read);
Socket so = new Socket("localhost",port);
so.getOutputStream().write(str.getBytes());
so.getOutputStream().flush();
so.getOutputStream().close();
so.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询