客户端 - 服务器不同的语言和操作系统

时间:2016-10-23 10:40:08

标签: java c linux windows sockets

我正在尝试修改客户端 - 服务器程序,以便我可以从我想在Windows中运行的客户端连接到用C语言编写的服务器(我在Linux上的VirtualBox中运行),并且用Java编写。

我有一些问题。这是我在Linux上运行的server.c文件:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>

void deservire_client(int c) {
  // serving the client
  int nr, i=2, nrDiv=0, sirDiv[10]={0,0,0,0,0,0,0,0,0,0};
  recv(c, &nr, sizeof(nr), MSG_WAITALL);
  while ( i <= nr/2){
            if ( nr%i == 0){
                sirDiv[nrDiv]=i;
                nrDiv+=1;
            }
            i+=1;
        }
  send(c, &sirDiv, sizeof(sirDiv), 0);
  close(c);
  // ending of serving the client;
}

int main() {
  int s;
  struct sockaddr_in server, client;
  int c, l;

  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    printf("Creating server socket error!\n");
    return 1;
  }

  memset(&server, 0, sizeof(server));
  server.sin_port = htons(1234);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;

  if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
    printf("Binding Error!\n");
    return 1;
  }

  listen(s, 5);

  l = sizeof(client);
  memset(&client, 0, sizeof(client));

  while (1) {
    c = accept(s, (struct sockaddr *) &client, &l);
    printf("S-a conectat un client.\n");
    if (fork() == 0) { // fiu
      deservire_client(c);
      return 0;
    }

  }
}

如您所见,服务器将返回一个整数数组,它将是我从客户端文件发送的数字的除数。

我已经在C中编写了一个客户端文件,只是为了查看它是否有效。我在Linux下运行它们。这是文件:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <unistd.h>

int main() {
  int c;
  struct sockaddr_in server;
  char send_data[1024];


  c = socket(AF_INET, SOCK_STREAM, 0);
  if (c < 0) {
    printf("Creating client socket error!\n");
    return 1;
  }

  memset(&server, 0, sizeof(server));
  server.sin_port = htons(1234);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr("127.0.0.1");

  if (connect(c, (struct sockaddr *) &server, sizeof(server)) < 0) {
    printf("Connecting to server error!\n");
    return 1;
  }

  while(1){
    int nr, sirDiv[10]={0,0,0,0,0,0,0,0,0,0}, i=0;
    printf("\nInput number: ");   scanf("%d",&nr);
    send(c ,&nr, sizeof(nr), 0);
    recv(c, &sirDiv, sizeof(sirDiv), 0);

    printf("\nThe divisors are:\n");
    while( i <= 10 ){
      if ( sirDiv[i] <= nr/2 && sirDiv[i] != 0)
        printf("%d ",sirDiv[i]);
      i+=1;
    }
    printf("\n");

    //Ask client if he wants to close
    printf("\nq or Q to exit: ");
    scanf("%s",send_data);
    if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0) {
      close(c);
      break;
    }
  }
  return 0;
}

所以这在Linux下工作正常,我可以将多个客户端连接到服务器。现在我想创建一个客户端并用Java编写它。这是一个给我的例子,我正在尝试修改:

import java.net.*;
import java.io.*;

public class Client2 {

    private static final String SERVER_ADDRESS = "127.0.0.1";
    private static final int SERVER_PORT = 1234;
    private static final int UNSIGNED_SHORT_MAX_VALUE = 65535;
    private static final int UNSIGNED_SHORT_MIN_VALUE = 0;

    public static void main(String args[]) {
        Socket socket = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
            int a = readUnsignedShort("a = ", reader);
            int b = readUnsignedShort("b = ", reader);
            writeIntegersToSocket(a, b, socket);
            readIntegersSumFromSocket(socket);
        } catch (IOException e) {
            System.err.println("Cautgh exception " + e.getMessage());
        } finally {
            closeStreams(socket,reader);
        }
    }

    private static void readIntegersSumFromSocket(Socket c) throws IOException {
        DataInputStream socketIn = new DataInputStream(c.getInputStream());
        int s = socketIn.readUnsignedShort();
        System.out.println("s = " + s);
    }

    private static void writeIntegersToSocket(int a, int b, Socket c) throws IOException {
        DataOutputStream socketOut = new DataOutputStream(c.getOutputStream());
        socketOut.writeShort(a);
        socketOut.writeShort(b);
        socketOut.flush();
    }

    private static int readUnsignedShort(String message, BufferedReader reader) throws IOException {
        int unsignedShortNumber = 0;
        System.out.print(message);
        try {
            unsignedShortNumber = Integer.parseInt(reader.readLine());
            if (unsignedShortNumber < UNSIGNED_SHORT_MIN_VALUE || unsignedShortNumber > UNSIGNED_SHORT_MAX_VALUE) {
                throw new IllegalArgumentException("The given number must be unsigned short [0..65535]!");
            }
        } catch (NumberFormatException e) {
            System.err.println("The given input is not an integer!");
        }
        return unsignedShortNumber;
    }

    private static void closeStreams(Socket socket, BufferedReader reader) {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                System.err.println("Could not close socket!");
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                System.err.println("Could not close reader!");
            }
        }
    }

}

此示例是针对客户端 - 服务器程序进行的,该程序允许用户输入两个数字,服务器将返回总和。当我尝试修改它时,我遇到了一些问题。

public static void main(String args[]) {
        Socket socket = null;
        int i = 0;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
            int nr = readUnsignedShort("nr = ", reader);
            while(int i <=10)
                int sirDiv[i] = readUnsignedShort("b = ", reader);//Here I have an error, type mismatch, how do I fix this?
            writeIntegersToSocket(nr, sirDiv, socket);//Again, how do I write this instead?
            readIntegersSumFromSocket(socket);

private static void writeIntegersToSocket(int a, int b, Socket c) throws IOException {
        DataOutputStream socketOut = new DataOutputStream(c.getOutputStream());
        socketOut.writeShort(nr);
        socketOut.writeShort(nrDiv); //will probably have an error here as well
        socketOut.flush();
    }

除了这些类型不匹配之外,我仍然不知道如何连接到我在linux上运行的服务器,我需要更改什么,SERVER_ADDRESS还是什么?在Linux中ping localhost给了我127.0.0.1并在Windows中ping“ping -4 localhost”给了我同样的东西。有人可以给我一些指示吗?我一整天都在努力。

0 个答案:

没有答案