troubleshooting Question

what the hey

Avatar of kayvey
kayvey asked on
Java
10 Comments1 Solution392 ViewsLast Modified:
oh mwan.  these sockets are boingy boingy.

here is command line happenings:

 9264  p1  I+     0:00.04 vi BankServer.java
> gcc bank_client.c -o bank
> java BankServer &
[1] 12575
> Bank is open!



Bank>

Bank>

./bank
teller received msg:
 null
Exception in thread "main" java.lang.NullPointerException
        at BankServer.main(BankServer.java:29)
Bus error (core dumped)


[end command line happenings]

here is java code

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

public class BankServer
{
  BankServer()
  {
    balance = 0;
  }
  private int balance;

  public static void main(String args[]) throws IOException
  {
    ServerSocket bankDoor = new ServerSocket(6669);
    String patronBlahBlah;
    System.out.println("Bank is open!\n");
    System.out.println("\n");
    System.out.println("Bank>\n");
    while (true)
    {
      System.out.println("Bank>\n");
      Socket teller = bankDoor.accept();
      BufferedReader patronReq =
       new BufferedReader (new InputStreamReader(
        teller.getInputStream()));
      patronBlahBlah = patronReq.readLine();
      System.out.println("teller received msg:\n "+patronBlahBlah);
      if (patronBlahBlah.equals("bye"))
      {
        System.out.println("teller says bye\n ");
        break;
      }
      else
      {
        System.out.println("bank staying open\n ");
      }

    }
    System.out.println("Bank closing\n");
  }

  private void deposit(int amount)
  {
    if (amount >= 0 )
    {
      balance += amount;
    }
    else
    {
      System.out.println("Use withdraw take money out.\n");
      System.out.println("$ ");
      System.out.println(amount);
      System.out.println(" not accepted as deposit\n");
    }
  }

  private void withdraw(int amount)
  {
    if (amount >= 0 )
    {
      if (amount <= balance)
      {
        balance -= amount;
      }
      else
      {
        System.out.println("Insufficient funds for withdrawl of\n");
        System.out.println("$ ");
        System.out.println(amount);
        System.out.println("Withdrawl not accepted.\n");
      }
    }
    else
    {
      System.out.println("Use deposit to get money.\n");
      System.out.println("$ ");
      System.out.println(amount);
      System.out.println(" not accepted as withdrawl\n");
    }    

  }
 
  private int balance()
  {
    return balance;
  }
}

[end java code]

here is c code

#define theSOCKET "bank_sock"
#define bankPORT 6669

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

int main (void)
{
  char buffer[1024];
  char * msg;
  int sock, length;
  char bankHost[64];
  struct hostent *ptrHost;
  struct sockaddr_in tellerWinder;

  /*
   * get localhost
  if (gethostname(bankHost, sizeof(bankHost)) < 0)
  {
    perror("can't get localhost\n");
    exit(45);
  }
   */

  /*
   * call post office to get zip
   */
  if ((ptrHost =  gethostbyname("localhost")) == NULL)
  {
    fprintf(stderr, "can't find host: %s \n",bankHost);
    exit(46);
  }

  /*
   *  attempt to create INET socket
   */
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("can't create socket\n");
    exit(42);
  }

  /*
   *  transcribe bank statement into Yahoo! maps
   */
  memset(&tellerWinder, 0, sizeof(struct sockaddr_in));
  tellerWinder.sin_family = AF_INET;
  tellerWinder.sin_port = htons(bankPORT);
  memcpy(&tellerWinder.sin_addr, ptrHost->h_addr_list[0], ptrHost->h_length );
  length = sizeof(struct  sockaddr_in) ;

  /*
   *  download Yahoo! map doc into cigarette lighter
   */
  if (connect(sock, (struct sockaddr *) &tellerWinder, length) < 0)
  {
    perror("can't connect to tellerWinder\n");
    exit(43);
  }


  /*
   * werking 9 to 5, what a way to make a living
   */
  msg = malloc(6);
  strcpy("hello",msg);
  printf ("msg is %s \n",msg);
  printf("in client before while\n");
 while (!strcmp(msg,"bye"))
  {
    free(msg);
    printf("doing while\n");
    length = read(0, buffer, sizeof(buffer));
    buffer[length]='\0';
    length += 1;
    msg=malloc(length);
    strcpy(buffer,msg);
    printf("client msg: %s\n",msg);
    if(send(sock, msg, length,0) < 0)
    {
      perror("client can't talk to tellerWinder\n");
      free(msg);
      exit(44);
    }
  }
  close(sock);
  exit(0);
}

[end c code ]

running java like this

java BankServer &

and c like this

./bank

umm..
ASKER CERTIFIED SOLUTION
Mick Barry
Java Developer
Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros