Getting "java.net.SocketException: Connection reset" while send a String data using ObjectOutputStream - socketexception

I'm trying to send a String data over TCP using ObjectOutputStream, but I always get "java.net.SocketException: Connection reset". Here is my code:
Client:
clientSocket = new Socket("127.0.0.1", 9000);
oo = new ObjectOutputStream(clientSocket.getOutputStream());
oo.writeObject(data);
oo.flush();
Server:
ServerSocket server = new ServerSocket(9000);
Socket client = server.accept();
ObjectInputStream oi = new ObjectInputStream(client.getInputStream());
String data = (String) oi.readObject();
System.out.println(data);

Related

Error while connecting to SQL Server. The server was not found or was not accessible

I am trying to connect to my SQL Server database. I am using C#. Here is my code:
string connectionString = #"Server = tcp:<myLocalIP>, 1433; Initial catalog = <DatabaseName>;
User Id = <User>; Password = <Passwd>;",
queryString = "SELECT * FROM [...]";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand SelectCommand = new SqlCommand(queryString, connection);
SqlDataReader myreader;
connection.Open();
myreader = SelectCommand.ExecuteReader();
List<String> lst = new List<String>();
while (myreader.Read())
{
lst .Add(myreader[0].ToString());
}
connection.Close();
But connection.Open() throws an error:
SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0).
My server is started, it allows remote connections and TCP/IP is enabled. Any ideas?
DBMSSOCN=TCP/IP is how to use TCP/IP instead of Named Pipes.
Try to change :
string connectionString = #"Server = tcp:<myLocalIP>, 1433;Network Library=DBMSSOCN; Initial catalog = <DatabaseName>;
User Id = <User>; Password = <Passwd>;"
If it won't work, try to hard core IP as in below example :
string connectionString = "Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword";
For more informations: Connection Strings
Hope that will solve problem, cheers
I found a solution. I created a server(Console Application) that connects to MySQL server. Then I connected my Xamarin/ASP.NET application to that server where I process queries.

JDBC connection from eclipse not working, but working in Oracle SQL developer

I'm trying to connect into DB via JDBC
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:#host_name:port:service_name"; //Changed the host_name, port and service_name for confidentiality
String username = "user_name"; // Changed the user_name
String password = "my_password"; //Changed the password
Connection conn = DriverManager.getConnection(url, username, password);
Got the below exception
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
But at the same time, I could connect via Oracle SQL developer.
Note- My service_name consists of "." and "_" and My host_name contains "."
Please help here!
//Step: 1
Load Driver Class
ex- Class.forName("oracle.jdbc.driver.OracleDriver");
//Step: 2
String url="jdbc:oracle:thin:#ipaddress:portNo of oracle :database name";
ex- String url="jdbc:oracle:thin:#localhost:1521:XE";
//Step: 3
User Name
String username = "user_name";
ex- String username = "system";
//Step: 3
Password
String password = "oracle_db_Password";
ex- String password = "sys123";
//Step:4
Connection con=DriverManager.getConnection(url,username,password);
Below is the right way to create data source object from oracle documentation:
//specify the datasource object
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#//myhost:1521/orcl");
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
I hope you are using it right way

C# "The request was aborted: Could not create SSL/TLS secure channel." - happening occasionally

This is a request to GoCardless test API from a Dynamics CRM plugin. I receive "The request was aborted: Could not create SSL/TLS secure channel." error. It only happens on the first request after some time without sending one. If I send it again, it will be OK. I would appreciate a lot your help.
Here is my code:
//I have tried all the following lines in comment without success
//ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
// Create a new WebClient instance.
string baseURL = "https://api-sandbox.gocardless.com/";
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Authorization", "Bearer " + t);
client.Headers.Add("GoCardless-Version", "2015-07-06");
client.Headers.Add("Accept", "application/json");
Customers model = new Customers();
customer.country_code = "GB";
model.customers = customer;
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Customers));
ser.WriteObject(stream1, model);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
// Apply ASCII Encoding to obtain the string as a byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(sr.ReadToEnd());
ReturnedCustomers result = new ReturnedCustomers();
//Upload the input string using the HTTP 1.0 POST method.
try
{
byte[] responseArray = client.UploadData(baseURL + "customers", "POST", byteArray);
string responseText = Encoding.ASCII.GetString(responseArray);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ReturnedCustomers));
using (Stream s = GenerateStreamFromString(responseText))
{
result = (ReturnedCustomers)serializer.ReadObject(s);
}
}
catch (WebException exception)
{
}
From the Microsoft documentation (https://msdn.microsoft.com/en-us/library/gg334752.aspx) are the following limitations:
Only the HTTP and HTTPS protocols are allowed.
Access to localhost (loopback) is not permitted.
IP addresses cannot be used. You must use a named web address that requires DNS name resolution.
Anonymous authentication is supported and recommended.
5.There is no provision for prompting the logged on user for credentials or saving those credentials.
The error may be due to seguneti things:
The certificate is invalid
The certification authority is not public
Could you check what is the value of ServicePointManager.Expect100Continue and ServicePointManager.SecurityProtocol attributes in your environment?

invalid_frame_end_marker error while publishing to RabbitMQ using .net client

I am using RabbitMQ .net client library to publish the messages to a RabbitMQ node in the following manner.
var factory = new ConnectionFactory
{
HostName = "localhost",
Port = 5672,
UserName = "guest",
Password = "guest",
VirtualHost = #"/"
};
_conn = factory.CreateConnection();
_channel = _conn.CreateModel();
_properties = _channel.CreateBasicProperties();
And then the below is being called in a loop
using (var memStream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(memStream, message);
var properties = _channel.CreateBasicProperties();
properties.Priority = Convert.ToByte((int) priority);
_channel.BasicPublish(String.Empty, _routeKey, properties, memStream.ToArray());
}
The above code is working fine with a medium load around 50-100 messages per second. But when I increase the number of messages to be published around 500 messages per second, the RabbitMQ node starts giving the below error and disconnects the channel.
Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=501, text="FRAME_ERROR - type 206, first 16 octets = <<31,0,60,0,40,0,0,6,115,101,110,115,111,114,16,115>>:
{invalid_frame_end_marker, 114}
", classId=0, methodId=0, cause=:
at RabbitMQ.Client.Impl.SessionBase.Transmit(Command cmd)
at RabbitMQ.Client.Impl.ModelBase.ModelSend(MethodBase method, ContentHeaderBase header, Byte[] body)
at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, Boolean mandatory, Boolean immediate, IBasicProperties basicProperties, Byte[] body)
at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, Byte[] body)
at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, IBasicProperties basicProperties, Byte[] body)`
The size of the message with BinaryFormatter is around 5kb.

Restlet WebService returning a PDF

I have a Restlet Web Service that is returning a PDF document and the client side needs to receive this docuement and display it.
Server Side Code:
orderNumber = f.getFirstValue(PARAM_ORDER_NUMBER);
orderType = f.getFirstValue(PARAM_ORDER_TYPE).toUpperCase();
String fileName = OrderDM.getPDF(orderNumber, orderType);
pdfRep = new FileRepresentation(new File(fileName), MediaType.APPLICATION_PDF, 0);
return pdfRep;
Client Side:
ClientResource client = getClientResource(PRINTER_FRIENDLY_DOCUMENT_RESOURCE);
Reference reference = client.getReference();
reference.addQueryParameter(PARAM_ORDER_TYPE, orderType);
reference.addQueryParameter(PARAM_ORDER_NUMBER, orderNumber);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
client.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();
Will this work and is this the best way to do this.