客户端。我在我的电脑上使用它,安装了MAMP,但一切似乎都很好。
package com.data.jdbc;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ClientDemo {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("127.0.0.1", 8888);
Scanner is = new Scanner(s.getInputStream()); //receive data from server
System.out.println("Enter the string");
Scanner scan = new Scanner(System.in);
String name = scan.next();
PrintStream p = new PrintStream(s.getOutputStream());
p.println(name);
String temp=is.next();
System.out.println(temp);
// TODO Auto-generated method stub
}
}
服务器端 我在我的电脑上使用它,安装了MAMP,但一切似乎都很好。
package com.data.jdbc;
import java.io.IOException;
import java.io.PrintStream;
import java.net.*;
import java.util.Scanner;
public class ServerDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket sc1 = new ServerSocket(8888);
Socket s11 = sc1.accept();
Scanner scan1 = new Scanner(s11.getInputStream());
String name1 = scan1.next();
String temp1= name1 + "Poudel";
PrintStream p1 = new PrintStream(s11.getOutputStream());
p1.println(temp1);
}
}
错误消息::我收到绑定错误。谁能解决我做错了地方
Exception in thread "main" java.net.BindException: Address already in use (Bind failed)
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at com.data.jdbc.ServerDemo.main(ServerDemo.java:18)
有人可以找到我的问题吗?