我正在尝试使用SOCKETS建立客户端/服务器连接。
服务器端是Eclipse中的Java, 客户端在Android Studio中。
服务器端和客户端都有一些代码用于套接字连接。
昨天连接成功了。今天,我尝试用android app客户端连接到服务器,我有FAİLED。
当我检查来自 netstat -a -n 的连接时,我看到了:
(当我打开客户端时)
这很奇怪,因为在连接代码工作之前我已经看过这个端口了。端口增加了。
和服务器代码:
public class CapitalizeServer {
static ArrayList<Client> clients = new ArrayList<>();
public static void main(String[] args) throws Exception {
System.out.println("The capitalization server is running.");
int clientNumber = 0;
ServerSocket listener = new ServerSocket(44057);
try {
while (true) {
new Capitalizer(listener.accept(), clientNumber++).start();
}
} finally {
listener.close();
}
}
private static class Capitalizer extends Thread {
private Socket socket;
private int clientNumber;
private String uname,pass;
private boolean checked;
public Capitalizer(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String input = in.readLine();
if(input.substring(0, "uname:".length()).equals("uname:")){
this.uname = input.substring(6);
}else{
return; //Programı durdur
}
input = in.readLine();
if(input.substring(0,"pass:".length()).equals("pass:")){
this.pass = input.substring(5);
}else{
return; //Programı durdur
}
if(this.pass != null && !checked){
if(checkPass() == 1){
//Yanıt olumlu olduğu için bağlantı clientlar arasına eklenir.
Client cc = new Client(uname, pass, socket, 0, 1);
clients.add(cc);
checked = true;
}else{
//Yanıt olumuz ise bağlantı koparılır.
socket.close();
log("Socket Closed!" + socket);
checked = true;
}
}
while (true) {
if (input == null || input.equals(".")) {
break;
}
//Mesajlar iletilir.
if(input.substring(0,3)=="com:"){
switch (input.substring(4,5)) {
case "1": //Attack başlat 1 şİMDİLİK SADECE BU SEÇENEK AKTİF
//pHp ile random person belirle. (uname döndürür.)
//clients arrayında olup olmadığını kontrol et.(online mı ve savaşta mı)
//->eğer online ise, savaşta olup olmadığına bak
// eğer savaşta ise saldırılmaz.
// Eğer savaşta değil ise saldır, socket ile onu da savaş alanına al
//->Eğer online değil ise savaşı başlat.Adama da mesaj at.
attack1();
break;
case "2": //Attack başlat 2
break;
case "3": //Attack başlat 3
break;
case "4": //Attack başlat 4
break;
default:
break;
}
}
}
} catch (IOException e) {
log("Error handling client# " + clientNumber + ": " + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
public void attack1(){
try {
// open a connection to the site
URL url = new URL("http://localhost/attack/attack.php");
URLConnection con = url.openConnection();
// activate the output
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
// send your parameters to your site
ps.print("firstKey=firstValue");
ps.print("&secondKey=secondValue");
// we have to get the input stream in order to actually send the request
//con.getInputStream();
// close the print stream
ps.close();
BufferedReader cin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = cin.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public int checkPass(){
//Connect to db and check the pass and uname
//Php kodları arasında check işlemini yapan url'yi çalıştır
//Yanıt olumlu ise 1, değilse 0 döndür.
try {
// open a connection to the site
URL url = new URL("http://localhost/userControls/singin.php");
URLConnection con = url.openConnection();
// activate the output
con.setDoOutput(true);
//con.setDoInput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
// send your parameters to your site
ps.print(URLEncoder.encode("uname","UTF-8") + "=" + URLEncoder.encode(uname, "UTF-8"));
ps.print(URLEncoder.encode("&pass=","UTF-8")+ "=" + URLEncoder.encode(pass, "utf-8"));
ps.close();
System.out.println("111111");
BufferedReader cin = new BufferedReader(new InputStreamReader(con.getInputStream()));
System.out.println("222222");
String line = null;
while ((line = cin.readLine()) != null) {
System.out.println(":::::::Geldik la Geldiiiik::::::::: " + line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
private void log(String message) {
System.out.println(message);
}
}
}
客户第一个活动代码:
public class SignInMenu extends ActionBarActivity {
Button button18;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin_menu);
button18 = (Button) findViewById(R.id.button18);
et1 = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
}
public void button18Click(View view){
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("uname",et1.getText().toString());
intent.putExtra("pass",et2.getText().toString());
startActivity(intent);
}
}
我正在使用WAMP服务器。 它有一些奇怪的东西。我无法使用我的本地IP(192.168.1.36)访问localhost。只有这样:http://localhost/
当我尝试连接我的本地IP时,错误如下:192.168.1.36拒绝连接。
也许这就是问题所在。
那么我该如何解决这个问题?
EDİT:(新问题)
*问题是关于java-php连接现在。
我的服务器连接到php查询。这是连接到php页面的代码。我在“PrintStream ps = new PrintStream(con.getOutputStream());”中出错。 错误说:连接被拒绝:连接
try{
// open a connection to the site
URL url = new URL("http://192.168.1.35/userControls/singin.php");
URLConnection con = url.openConnection();
// activate the output
con.setDoOutput(true);
//con.setDoInput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
// send your parameters to your site
ps.print(URLEncoder.encode("uname","UTF-8") + "=" + URLEncoder.encode(uname, "UTF-8"));
ps.print(URLEncoder.encode("&pass=","UTF-8")+ "=" + URLEncoder.encode(pass, "utf-8"));
ps.close();
BufferedReader cin = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = cin.readLine()) != null) {
System.out.println(":::::::Worked::::::::: " + line);
}
}/* catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/ catch (Exception e) {
System.out.println("Eror:" + e.getMessage());
}
我如何解决第二个问题?